English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes php+Ajax implements the upload image function with a progress bar. Shared for everyone's reference, as follows:
The running effect is as follows:
The code is as follows:
<?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 encounter errors or a blank page if "memory_limit" or "upload_max_filesize" is set too 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 extension $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.'); } die('error uploading File!'); } } else { die('Something wrong with upload! Is "upload_max_filesize" set correctly?'); }
Complete example code click hereSite Download.
Readers interested in more PHP-related content can check out the special topics on this site: PHP File Operation Summary, PHP Operation and Operator Usage Summary, PHP Network Programming Skills Summary, PHP Basic Syntax Tutorial, PHP Operation Office Document Skills Summary (including word, excel, access, ppt), PHP Date and Time Usage Summary, PHP Object-Oriented Program Design Tutorial, PHP String (string) Usage Summary, PHP+MySQL Database Operation Tutorial and PHP Common Database Operation Skills Summary
I hope this article is helpful to everyone's PHP program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @) to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content involved.