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 相关文章推荐
php下载远程文件类(支持断点续传)
Nov 14 PHP
php 3行代码的分页算法(求起始页和结束页)
Oct 21 PHP
PHP学习笔记之一
Jan 17 PHP
php递归创建和删除文件夹的代码小结
Apr 13 PHP
PHP的5个安全措施小结
Jul 17 PHP
深入php-fpm的两种进程管理模式详解
Jun 03 PHP
深入Nginx + PHP 缓存详解
Jul 11 PHP
php sybase_fetch_array使用方法
Apr 15 PHP
Codeigniter实现多文件上传并创建多个缩略图
Jun 12 PHP
PHP管理依赖(dependency)关系工具 Composer 安装与使用
Aug 18 PHP
php缓冲输出实例分析
Jan 05 PHP
php rsa加密解密使用详解
Jan 14 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生成随机用户名和密码的实现代码
2013/02/27 PHP
PHP导航下拉菜单的实现如此简单
2013/09/22 PHP
PHP中获取时间的下一周下个月的方法
2014/03/18 PHP
php cookie名使用点号(句号)会被转换
2014/10/23 PHP
PHP获取类私有属性的3种方法
2020/09/10 PHP
javascript 面向对象编程基础:继承
2009/08/21 Javascript
jquery移除、绑定、触发元素事件使用示例详解
2014/04/10 Javascript
Google Maps API地图应用示例分享
2014/10/23 Javascript
jQuery中:reset选择器用法实例
2015/01/04 Javascript
jQuery选择器源码解读(八):addCombinator函数
2015/03/31 Javascript
JavaScript中数据结构与算法(一):栈
2015/06/19 Javascript
AngularJS 表达式详解及实例代码
2016/09/14 Javascript
微信小程序Redux绑定实例详解
2017/06/07 Javascript
微信小程序 监听手势滑动切换页面实例详解
2017/06/15 Javascript
js动态引入的四种方法
2018/05/05 Javascript
vue单页开发父子组件传值思路详解
2018/05/18 Javascript
javascript json对象小技巧之键名作为变量用法分析
2019/11/11 Javascript
[06:53]2018DOTA2国际邀请赛寻真——勇于创新的Vici Gaming
2018/08/14 DOTA
python TKinter获取文本框内容的方法
2018/10/11 Python
python文档字符串(函数使用说明)使用详解
2019/07/30 Python
python如何将多个PDF进行合并
2019/08/13 Python
导入tensorflow时报错:cannot import name 'abs'的解决
2019/10/10 Python
浅谈keras使用预训练模型vgg16分类,损失和准确度不变
2020/07/02 Python
ffmpeg+Python实现B站MP4格式音频与视频的合并示例代码
2020/10/21 Python
html5使用canvas画一条线
2014/12/15 HTML / CSS
Origins悦木之源英国官网:雅诗兰黛集团高端植物护肤品牌
2017/11/06 全球购物
美国在线购买内衣网站:HerRoom
2020/02/22 全球购物
毕业生个人求职信范文分享
2014/01/05 职场文书
不假外出检讨书
2014/01/27 职场文书
运动会广播稿20字
2014/02/18 职场文书
《三个小伙伴》教学反思
2014/04/11 职场文书
2014年秋季开学寄语
2014/08/02 职场文书
个人授权委托书样本
2014/09/13 职场文书
教师个人考察材料
2014/12/16 职场文书
工作检讨书大全
2015/01/26 职场文书
JavaScript展开运算符和剩余运算符的区别详解
2022/02/18 Javascript