IntroductionPHP Ajax comes with the feature of upload files, as easy as read a simple input. This feature is done by a trick which is explained bellow. DetailsAjax simulationAs you should know, you cannot upload files with regular ajax request. For simulate that, this php project simulate an ajax request by generating a form, then the form is submitted into an iframe. When the request was done, and it was processed by phpajax, the generated form is cleaned from page. All those steps, are generated by the javascript that comes with the project, the web developer only need to define that he wants to read an input with a type of "file". Example<?php
require("phpajax.php");
class file_handler extends phpajax {
function input() {
aread("file1");
}
function loading() {
aprintf("loading", "Uploading a file");
ashow("loading");
}
function main() {
alert("The file ". $this->file1." is uploaded ");
//do something with
//$_FILE["file1"];
}
}
phpajax::init();
?><html>
<head>
<title>Example of how upload a file</title>
<?php phpajax_js("../phpajax/");?>
</head>
<body>
<div id='loading' style="visibility:hidden;"></div>
<input type="file" name="file1" id="file1">
<a href="javascript:file_handler()">Uploading the file</a>
</body>
</html>As you can see upload a file is so easy, an also it is asynchronous, this means that you can upload other file while the uploading of a previous file wasn't finish yet.
|