欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-06-03 21:47:03
...
这篇文章主要介绍了php+ajax实现带进度条的上传图片功能,涉及php文件传输及ajax无刷新提交的相关操作技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下

运行效果图如下:

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

代码如下:

<?php
if(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?');
}

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP实现的自定义数组排序函数与排序类

PHP实现批量获取网页中所有固定种子链接的方法

PHP实现二维数组按某列进行排序的方法_php技巧

以上就是php+ajax实现带进度条的上传图片功能实例详解的详细内容,更多请关注其它相关文章!