php+ajax實現帶進度條的上傳圖片功能執行個體詳解

來源:互聯網
上載者:User
這篇文章主要介紹了php+ajax實現帶進度條的上傳圖片功能,涉及php檔案傳輸及ajax無重新整理提交的相關操作技巧,並附帶demo源碼供讀者下載參考,需要的朋友可以參考下

運行如下:

代碼如下:

<?phpif(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK){  ############ Edit settings ##############  $UploadDirectory  = 'F:/Websites/file_upload/uploads/'; //specify upload directory ends with / (slash)  ##########################################  /*  Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".  Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit  and set them adequately, also check "post_max_size".  */  //check if this is an ajax request  if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){    die();  }  //Is file size is less than allowed size.  if ($_FILES["FileInput"]["size"] > 5242880) {    die("File size is too big!");  }  //allowed file type Server side check  switch(strtolower($_FILES['FileInput']['type']))    {      //allowed file types      case 'image/png':      case 'image/gif':      case 'image/jpeg':      case 'image/pjpeg':      case 'text/plain':      case 'text/html': //html file      case 'application/x-zip-compressed':      case 'application/pdf':      case 'application/msword':      case 'application/vnd.ms-excel':      case 'video/mp4':        break;      default:        die('Unsupported File!'); //output error  }  $File_Name     = strtolower($_FILES['FileInput']['name']);  $File_Ext      = substr($File_Name, strrpos($File_Name, '.')); //get file extention  $Random_Number   = rand(0, 9999999999); //Random number to be added to name.  $NewFileName    = $Random_Number.$File_Ext; //new file name  if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName ))    {    die('Success! File Uploaded.');  }else{    die('error uploading File!');  }}else{  die('Something wrong with upload! Is "upload_max_filesize" set correctly?');}

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.