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

php+ajax实现带进度条的上传图片功能【附demo源码下载】

程序员文章站 2024-04-02 15:01:52
本文实例讲述了php+ajax实现带进度条的上传图片功能。分享给大家供大家参考,具体如下: 运行效果图如下: 代码如下:

本文实例讲述了php+ajax实现带进度条的上传图片功能。分享给大家供大家参考,具体如下:

运行效果图如下:

php+ajax实现带进度条的上传图片功能【附demo源码下载】

代码如下:

<?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基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。