最新消息: 新版网站上线了!!!

php ajax带度条上传图片实例

  1. <?php 
  2.  
  3. if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK) 
  4. { 
  5.     ############ Edit settings ############## 
  6.     $UploadDirectory    = 'F:/Websites/file_upload/uploads/'; //specify upload directory ends with / (slash) 
  7.     ########################################## 
  8.      
  9.     /* 
  10.     Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".  
  11.     Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit  
  12.     and set them adequately, also check "post_max_size". 
  13.     */ 
  14.      
  15.     //check if this is an ajax request 
  16.     if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){ 
  17.         die(); 
  18.     } 
  19.      
  20.      
  21.     //Is file size is less than allowed size. 
  22.     if ($_FILES["FileInput"]["size"] > 5242880) { 
  23.         die("File size is too big!"); 
  24.     } 
  25.      
  26.     //allowed file type Server side check 
  27.     switch(strtolower($_FILES['FileInput']['type'])) 
  28.         { 
  29.             //allowed file types 
  30.             case 'image/png':  
  31.             case 'image/gif':  
  32.             case 'image/jpeg':  
  33.             case 'image/pjpeg': 
  34.             case 'text/plain': 
  35.             case 'text/html': //html file 
  36.             case 'application/x-zip-compressed': 
  37.             case 'application/pdf': 
  38.             case 'application/msword': 
  39.             case 'application/vnd.ms-excel': 
  40.             case 'video/mp4': 
  41.                 break; 
  42.             default: 
  43.                 die('Unsupported File!'); //output error 
  44.     } 
  45.      
  46.     $File_Name          = strtolower($_FILES['FileInput']['name']); 
  47.     $File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention 
  48.     $Random_Number      = rand(0, 9999999999); //Random number to be added to name. 
  49.     $NewFileName        = $Random_Number.$File_Ext; //new file name 
  50.      
  51.     if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName )) 
  52.        { 
  53.         die('Success! File Uploaded.'); 
  54.     }else{ 
  55.         die('error uploading File!'); 
  56.     } 
  57.      
  58. } 
  59. else 
  60. { 
  61.     die('Something wrong with upload! Is "upload_max_filesize" set correctly?'); 
  62. } 

 

转载请注明:谷谷点程序 » php ajax带度条上传图片实例