PHP file upload and download source code,

Source: Internet
Author: User
Tags php file upload ranges

PHP file upload and download source code,

  I. File Upload

Front-end page:

<! DOCTYPE html> 

  Background processing:

<? Php // receive and submit information sent on the page $ name = $ _ POST ['username']; $ intro =$ _ POST ['fileintro']; // $ _ FILES stores file-related information // echo "<pre>"; // print_r ($ _ FILES); // echo "</pre> "; // you can limit the file size here/* $ filesize = $ _ FILES ['myfile'] ['SIZE']; if ($ filesize> 2*1024*1024) {echo "the file is too large to be uploaded"; exit ();} * /// you can restrict the file type./* $ filetype = $ _ FILES ['myfile'] ['type']; if ($ filetype! = 'Image/jpg '& $ filetype! = 'Application/pdf ') {echo "file types can only be jpg and pdf"; exit ();} */if (is_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name']) {// save the file to the directory you want to store $ uploaded = $ _ FILES ['myfile'] ['tmp _ name']; // each user dynamically creates a folder $ userpath =$ _ SERVER ['document _ root']. "/up /". $ name; // determine whether the user already has a folder if (! File_exists ($ userpath) {mkdir ($ userpath);} // prevents the same user from uploading files of the same name. You can add a timestamp to the file. // $ Moveto = $ userpath. "/". time (). $ _ FILES ['myfile'] ['name']; // or modify the file name, however, the file suffix $ truename = $ _ FILES ['myfile'] ['name']; $ moveto = $ userpath must be used for string processing. "/". time (). substr ($ truename, strrpos ($ truename ,". "); if (move_uploaded_file ($ uploaded, iconv (" UTF-8 "," gb2312 ", $ moveto) {echo" Upload File ". $ _ FILES ['myfile'] ['name']. "succeeded";} else {echo "uploading files ". $ _ FILES ['myfile'] ['name']. "failed" ;}} else {echo "Upload File ". $ FILES ['myfi Le '] ['name']. "failed" ;}?>

  Ii. File Download:

Single file download:

<? Php function down_file ($ file_name, $ file_path) {// $ file_name = iconv ("uft-8", "gb2312", $ file_name); // if the file name is Chinese, you need to transcode the Chinese name gb2312 // the file to be downloaded and read to the server's memory // The server returns the file data to the browser // the browser writes the file to the specified location // 1. determine whether the object exists if (! File_exists ($ file_name) {echo "111"; return ;}$ fp = fopen ($ file_name, "r"); $ file_size = filesize ($ file_name ); // get the file size // you can use file_size to limit the size of the file downloaded by the browser. // The returned File header ("Content-type: application/octet-stream"); // The returned header ("Accept-Ranges: bytes") in bytes "); // return the file size header ("Accept-Length: $ file_size"); // In the displayed dialog box, the corresponding file name header ("Content-Disposition: attachment; filename = ". $ file_name); $ buffer = 1024; // defines the buffer // for download security, it is best to use the file byte read counter $ file_count = 0; // feof is used to determine whether the file is read to the end of the document while (! Feof ($ fp) & ($ file_size-$ file_count> 0) {$ file_data = fread ($ fp, $ buffer ); // count the number of bytes read $ file_count + $ buffer; echo $ file_data; // send some data to the browser} fclose ($ fp); // close the file}?>

  Download multiple files:

DownList. php:

<a href="downprocess.php?filename=1.jpg">down</a><br><br><a href="downprocess.php?filename=2.jpg">down</a><br><br><a href="downprocess.php?filename=3.jpg">down</a><br><br><a href="downprocess.php?filename=4.jpg">down</a><br><br>

  Downprocess. php:

<? Php $ filename = $ _ REQUEST ['filename']; function down_file ($ file_name) {if (! File_exists ($ file_name) {echo "111"; return ;}$ fp = fopen ($ file_name, "r"); $ file_size = filesize ($ file_name ); header ("Content-type: application/octet-stream"); // return header ("Accept-Ranges: bytes") in bytes "); // return the file size header ("Accept-Length: $ file_size"); // In the displayed dialog box, the corresponding file name header ("Content-Disposition: attachment; filename = ". $ file_name); $ buffer = 1024; // defines the buffer $ file_count = 0; // feof is used to determine whether the file is read to the end of the document while (! Feof ($ fp) & ($ file_size-$ file_count> 0) {$ file_data = fread ($ fp, $ buffer ); // count the number of bytes read $ file_count + $ buffer; echo $ file_data; // send some data to the browser} fclose ($ fp );} down_file ($ filename);?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.