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


Posted in PHP onSeptember 14, 2016

本文实例讲述了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 相关文章推荐
php5数字型字符串加解密代码
Apr 24 PHP
Windows IIS PHP 5.2 安装与配置方法
Jun 08 PHP
判断是否为指定长度内字符串的php函数
Feb 16 PHP
PHP UTF8中文字符截断函数代码
Sep 11 PHP
php存储过程调用实例代码
Feb 03 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
Jun 03 PHP
ThinkPHP中的常用查询语言汇总
Aug 22 PHP
Yii使用find findAll查找出指定字段的实现方法
Sep 05 PHP
Mac系统下使用brew搭建PHP(LNMP/LAMP)开发环境
Mar 03 PHP
PHP的new static和new self的区别与使用
Nov 27 PHP
TP5框架页面跳转样式操作示例
Apr 05 PHP
PHP $O00OO0=urldecode &amp; eval 解密,记一次商业源码的去后门
Sep 13 PHP
PHP自定义函数实现格式化秒的方法
Sep 14 #PHP
PHP经典算法集锦【经典收藏】
Sep 14 #PHP
微信支付的开发流程详解
Sep 13 #PHP
PHP仿微信多图片预览上传实例代码
Sep 13 #PHP
微信支付PHP SDK ―― 公众号支付代码详解
Sep 13 #PHP
PHP基于单例模式编写PDO类的方法
Sep 13 #PHP
利用PHP将图片转换成base64编码的实现方法
Sep 13 #PHP
You might like
php获取通过http协议post提交过来xml数据及解析xml
2012/12/16 PHP
深入php list()函数的详解
2013/06/05 PHP
ThinkPHP3.1新特性之命名范围的使用
2014/06/19 PHP
php中数据库连接方式pdo和mysqli对比分析
2015/02/25 PHP
php实现基于PDO的预处理示例
2017/03/28 PHP
通过php动态传数据到highcharts
2017/04/05 PHP
laravel 操作数据库常用函数的返回值方法
2019/10/11 PHP
JS字符串处理实例代码
2013/08/05 Javascript
判断js对象是否拥有某一个属性的js代码
2013/08/16 Javascript
jquery单行文字向上滚动效果的实现代码
2014/09/05 Javascript
angularjs基础教程
2014/12/25 Javascript
使用jQuery实现图片遮罩半透明坠落遮挡
2015/03/16 Javascript
微信小程序 限制1M的瘦身技巧与方法详解
2017/01/06 Javascript
微信小程序开发(二)图片上传+服务端接收详解
2017/01/11 Javascript
JavaScript正则表达式简单实用实例
2017/06/23 Javascript
微信JSSDK调用微信扫一扫功能的方法
2017/07/25 Javascript
vue获取DOM元素并设置属性的两种实现方法
2017/09/30 Javascript
vue2.0s中eventBus实现兄弟组件通信的示例代码
2017/10/25 Javascript
在React 组件中使用Echarts的示例代码
2017/11/08 Javascript
React+Antd+Redux实现待办事件的方法
2019/03/14 Javascript
[06:10]6.81新信使新套装!给你一个炫酷的DOTA2
2014/05/06 DOTA
[01:07]DOTA2次级职业联赛 - Fpb战队宣传片
2014/12/01 DOTA
Python struct模块解析
2014/06/12 Python
Python3基础之条件与循环控制实例解析
2014/08/13 Python
python3.5使用tkinter制作记事本
2016/06/20 Python
windows下安装Python的XlsxWriter模块方法
2018/05/03 Python
python实现一个简单的udp通信的示例代码
2019/02/01 Python
python通过opencv实现图片裁剪原理解析
2020/01/19 Python
python操作链表的示例代码
2020/09/27 Python
详解CSS 3 中的 calc() 方法
2018/01/12 HTML / CSS
HTML5实现页面切换激活的PageVisibility API使用初探
2016/05/13 HTML / CSS
汤米巴哈马官方网站:Tommy Bahama
2017/05/13 全球购物
SportsDirect.com新加坡:英国第一体育零售商
2019/03/30 全球购物
自我评价的写作规则
2014/01/06 职场文书
品质管理部岗位职责范文
2014/03/01 职场文书
外贸英文求职信范文
2015/03/19 职场文书