首頁 >後端開發 >php教程 >php+ajax實現帶有進度條的上傳圖片功能實例詳解

php+ajax實現帶有進度條的上傳圖片功能實例詳解

墨辰丷
墨辰丷原創
2018-06-01 11:16:531635瀏覽

這篇文章主要介紹了php ajax實現帶進度條的上傳圖片功能,涉及php文件傳輸及ajax無刷新提交的相關操作技巧,並附帶demo源碼供讀者下載參考,需要的朋友可以參考下

運行效果圖如下:

程式碼如下:

##

<?php
if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
  ############ Edit settings ##############
  $UploadDirectory  = &#39;F:/Websites/file_upload/uploads/&#39;; //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[&#39;HTTP_X_REQUESTED_WITH&#39;])){
    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[&#39;FileInput&#39;][&#39;type&#39;]))
    {
      //allowed file types
      case &#39;image/png&#39;:
      case &#39;image/gif&#39;:
      case &#39;image/jpeg&#39;:
      case &#39;image/pjpeg&#39;:
      case &#39;text/plain&#39;:
      case &#39;text/html&#39;: //html file
      case &#39;application/x-zip-compressed&#39;:
      case &#39;application/pdf&#39;:
      case &#39;application/msword&#39;:
      case &#39;application/vnd.ms-excel&#39;:
      case &#39;video/mp4&#39;:
        break;
      default:
        die(&#39;Unsupported File!&#39;); //output error
  }
  $File_Name     = strtolower($_FILES[&#39;FileInput&#39;][&#39;name&#39;]);
  $File_Ext      = substr($File_Name, strrpos($File_Name, &#39;.&#39;)); //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[&#39;FileInput&#39;][&#39;tmp_name&#39;], $UploadDirectory.$NewFileName ))
    {
    die(&#39;Success! File Uploaded.&#39;);
  }else{
    die(&#39;error uploading File!&#39;);
  }
}
else
{
  die(&#39;Something wrong with upload! Is "upload_max_filesize" set correctly?&#39;);
}

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

相關推薦:

PHP實作的自訂陣列排序函數與排序類別

##PHP實作批次取得網頁中所有固定種子連結的方法


PHP實作二維陣列按某列進行排序的方法_

php技巧

以上是php+ajax實現帶有進度條的上傳圖片功能實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn