首页 >后端开发 >php教程 >php+ajax实现带进度条的上传图片功能实例详解

php+ajax实现带进度条的上传图片功能实例详解

墨辰丷
墨辰丷原创
2018-06-01 11:16:531622浏览

这篇文章主要介绍了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