PHP file upload and download source code, _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
Php file upload and download source code ,. Php file upload and download source code. 1. file Upload foreground page :! DOCTYPEhtmlhtmlheadmetacharsetutf-8title file Upload titleheadbodydivforw.hodposta php file upload and download source code,

  I. file Upload

Front-end page:

    
     File Upload

User name
File introduction

Select the file to Upload

  Background processing:

 "; // Print_r ($ _ FILES); // echo"
"; // You can limit the file size here/* $ filesize = $ _ FILES ['myfile'] ['size']; if ($ filesize> 2*1024*1024) {echo "the file is too large to upload"; 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:

 
 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:

down

down

down

down

  Downprocess. php:

 0) {$ file_data = fread ($ fp, $ buffer); // count the number of bytes read $ file_count + $ buffer; echo $ file_data; // send part of the data to the browser} fclose ($ fp);} down_file ($ filename);?>

Upload, I. file upload front-end page :! DOCTYPE html head meta charset = "UTF-8" title file upload/title/head body p form method = "post"...

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.