Home > Backend Development > PHP Tutorial > ajax+php file upload code without refresh_PHP tutorial

ajax+php file upload code without refresh_PHP tutorial

WBOY
Release: 2016-07-13 17:06:51
Original
1108 people have browsed it

If you don’t use Ajax to upload files in PHP, the effect will not be very good and the user experience will not be very good. Let’s take a look at the Ajax+PHP file upload code without refreshing. ajax+php file upload code without refresh

ajax+php tutorial file upload code without refreshing




ajax+php file upload code without refreshing







                                                                                                                                                                                                         



            



                                                                                                                                                                                                                                               
Add file:

                                                                                                                                                                                          & Lt; td & gt; file path & lt;/td & gt;
                                                                                                                                                                                                                      
                                                              
                                                                                                                            
                                                                                                                                                                               


     
     
       
     
   
 
Warm reminder: Up to files can be uploaded at the same time. Only file.

             
         
       




file.php file

$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
$uploaddir ='./www.bKjia.c0m/';
                 $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';
 
 

               }

else{

$_msg=$_msg.$f_name.'Upload failed';

}
          }

}
echo "<script>window.parent.finish('".$_msg."');</script>";
/*
For PHP file upload, if Ajax is not used, the effect will not be very good, and the user experience will not be very good. Let’s take a look at this Ajax+PHP file upload code without refreshing.
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630650.htmlTechArticleUploading php files, if you don’t use ajax to implement it, the effect is not very good, and the user experience is not very good. Okay, let's take a look at this ajax+php no-refresh file upload code. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template