There are several asynchronous file upload functions commonly used, such as using the IFRAME framework form, Ajax function effect, and flash+php function, the following describes the Ajax and IFRAME implementation of asynchronous file Upload function example.
Method one, using jquery ajaxfileupload.js to implement file upload
In fact, the implementation of non-refresh file upload. Can use the iframe file upload principle.
In fact, when uploading files with PHP ... Can only be used in the form of $_files, but if we are only a single JS way to take its ID, such as.. document.getElementById (' img '). $ ("#img") in the form of value or jquery is not really true (but there are a lot of people doing it, and I did it at first).
But what if the function requires the implementation of so-called "asynchronous uploads"? Use only third-party components, or write one yourself (embed an IFRAME in a Web page). But if it is considered development time, it is recommended to use third party, here is a nice jquery ajax file to upload the component, namely "Ajaxfileupload.js", its component download address is: http://files.jb51.net/file_images/ Article/201306/js/ajaxfileupload.js
Process:
(1) Front-end file code: test.php
Copy the Code code as follows:
The corresponding HTML is:
Upload
So the client is done.
(2) doajaxfileupload.php on the server side
Here for the sake of simple detection whether the real value came over, you can save it up.
Copy the Code code as follows: $file _infor = Var_export ($_files,true);
File_put_contents ("d:file_infor.php". $file _infor);
So when you hit the file_infor.php file you just generated, you see the familiar information:
Copy the Code code as follows: Array (
' Name ' = ' lamp.jpg ',
' Type ' = ' image/pjpeg ',
' Tmp_name ' = ' c:windowstempphpfa.tmp ',
' Error ' =>0,
' Size ' =>3127
)
Of course, the real deal class is in this way:
Copy the Code code as follows: $upFilePath = "d:/";
$ok = @move_uploaded_file ($_files[' img '] [' tmp_name '], $upFilePath);
if ($ok = = = FALSE) {
echo json_encode (' file_infor ' = ' upload failed ');
}else{
echo json_encode (' file_infor ' = ' upload succeeded ');
}
?>
Method Two, upload the image using the IFRAME framework
The HTML code is as follows:
Copy the Code code as follows:
Index.js file:
Copy the Code code as follows: $ (function () {
$ ("#upload_file"). Change (function () {
$ ("#uploadFrom"). Submit ();
});
});
function Stopsend (str) {
var im= "";
$ ("#msg"). append (IM);
}
upload.php file:
Copy the Code code as follows: $file =$_files[' upfile '];
$name =rand (0,500000). Dechex (rand (0,10000)). ". JPG ";
Move_uploaded_file ($file [' Tmp_name '], "upload/images/". $name);
Invoking the JS function of the IFRAME parent window
echo "";
?>
Method Three, the original eco-ajax file Upload
Copy the Code code as follows:
HTML5 Ajax Uploading files
Image
Browse ...
Upload
PHP Code:
Copy the Code code as follows:
if (Isset ($_files["myfile"))
{
$ret = Array ();
$uploadDir = ' images '. Directory_separator.date ("Ymd"). Directory_separator;
$dir = DirName (__file__). Directory_separator. $uploadDir;
File_exists ($dir) | | (MkDir ($dir, 0777,true) && chmod ($dir, 0777));
if (!is_array ($_files["myfile" ["Name"]))//single file
{
$fileName = Time (). Uniqid (). '. '. PathInfo ($_files["myfile" ["Name"]) [' extension '];
Move_uploaded_file ($_files["myfile" ["Tmp_name"], $dir. $fileName);
$ret [' file '] = Directory_separator. $uploadDir. $fileName;
}
echo Json_encode ($ret);
}
?>
http://www.bkjia.com/PHPjc/825358.html www.bkjia.com true http://www.bkjia.com/PHPjc/825358.html techarticle at present, there are several asynchronous file upload functions, such as using the IFRAME framework form, Ajax function effect, as well as the flash+php function, the following describes Ajax and IFRAME implementation asynchronous file ...