php 带进度条上传图片,怎么在php中使用ajax实现一个带进度条的上传图片功能

怎么在php中使用ajax实现一个带进度条的上传图片功能

发布时间:2021-01-28 15:59:42

来源:亿速云

阅读:63

作者:Leah

这篇文章将为大家详细讲解有关怎么在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中使用ajax实现一个带进度条的上传图片功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值