In the past, we would all do ajax+php to implement file upload, so that the user can dynamically upload images without refreshing the page, which improved the user experience. At that time, the most commonly used method was to use iframe to operate. Let me introduce the actual operation. Ajax php image upload example.
I have been studying ajax+php form refresh verification before, which is mainly used in registration and submission forms. The use of ajax technology greatly increases the friendliness of visitors to web pages. As the most important technology to improve page friendliness, ajax is indispensable. Less.
Of course, ajax is not only the non-refresh verification of the form, but can also be better applied to other places on the page. Ajax technology is basically used in any place where there is no refresh. Today we are discussing ajax+php no refresh Upload pictures.
The technology of uploading pictures without refreshing is often used to upload attachments or pictures, such as the common QQ mailbox upload attachments, 163 mailbox upload attachments, QQ space upload pictures, etc. These types of applications use ajax non-refresh technology, which can Let us see the uploaded attachments on the current page, but in the background of ordinary small sites, when uploading products, we often cannot see the uploaded product images, which often does not achieve good results.
The following is the source code of an ajax+php non-refresh image upload. This is just an example. Modifying this example can be well applied to web pages, adding a non-refresh image upload to your page. Effects, there are many such effects, you can search them online.
file.php file
The code is as follows | Copy code | ||||
$sort=12; $f_type=strtolower("swf,jpg,rar,zip,7z,iso,gif");//Set the file types that can be uploaded $file_size_max=200*1024*1024;//Limit the maximum capacity of a single file upload $overwrite = 0;//Whether it is allowed to overwrite the same file, 1: allowed, 0: not allowed $f_input="Files";//Set the upload domain name foreach($_FILES[$f_input]["error"] as $key => $error){ $up_error="no"; If ($error == UPLOAD_ERR_OK){ $f_name=$_FILES[$f_input]['name'][$key];//Get the upload source file name $uploadfile=$uploaddir.strtolower(basename($f_name)); $tmp_type=substr(strrchr($f_name,"."),1);//Get the file extension $tmp_type=strtolower($tmp_type); If(!stristr($f_type,$tmp_type)){ echo "<script>alert('Sorry, cannot upload ".$tmp_type." format file, ".$f_name." file upload failed!')</script>"; $up_error="yes"; } If ($_FILES[$f_input]['size'][$key]>$file_size_max) { echo "<script>alert('Sorry, the file you uploaded ".$f_name." has a capacity of ".round($_FILES[$f_input]<br> ['size'][$key]/1024)."Kb, greater than the specified".($file_size_max/1024)."Kb, upload failed!')</script>"; $up_error="yes"; } If (file_exists($uploadfile)&&!$overwrite){ echo "<script>alert('Sorry, the file ".$f_name." already exists, upload failed!')</script>"; $up_error="yes"; } $string = 'abcdefghijklmnopgrstuvwxyz0123456789'; $rand = ''; for ($x=0;$x<12;$x++) $rand .= substr($string,mt_rand(0,strlen($string)-1),1); $t=date("ymdHis").substr($gettime[0],2,6).$rand; $attdir="./file/"; If(!is_dir($attdir)) { mkdir($attdir);} $uploadfile=$attdir.$t.".".$tmp_type; If(($up_error!="yes") and (move_uploaded_file($_FILES[$f_input]['tmp_name'] [$key], $uploadfile))){
$_msg=$_msg.$f_name.'Upload successful'; |
The code is as follows | Copy code |
|