"Code Learning" php File upload and download

Source: Internet
Author: User
Tags explode php file upload

=====================

File Upload and download

=====================

I. Configuration information for php.ini

File_uploads = ON/off Allow file upload
UPLOAD_MAX_FILESIZE=2M the maximum size of uploaded files
Post_max_size = maximum size allowed for 8M post data
Upload_tmp_dir the temporary directory where the uploaded files are placed

Second, the client upload settings

1, the form must be post submission
2. Upload type: enctype= "Multipart/form-data"
3, upload the use of the form items
<input type= "File" name= ":" >
4. (optional) Upload size limit form hidden fields: max_file_size,
<input type= "hidden" name= "max_file_size" value= "size byte"/>
Note: This field must precede the File entry field (usually after the form tag)

<HTML><Head><title>File Upload</title></Head><Body>    <formAction= ' upload.php 'Method= ' POST 'enctype= ' Multipart/form-data '>        <inputtype= ' hidden 'name= ' max_file_size 'value= ' 1000000 '>Select File:<inputtype= ' file 'name= ' myfile '>        <inputtype= ' Submit 'value= ' upload file '>    </form></Body></HTML>

Third, on the server side through the PHP processing upload

1. Use $_files global array to receive upload information
In each uploaded file, there are 5 attributes in the $_files:
Error: The wrong number of uploads: 0--4
0: Indicates that no errors have occurred.
1: The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
2: Indicates that the upload file size exceeds the maximum value specified by the Max_file_size element of the HTML form's hidden field property.
3: Indicates that the file is only partially uploaded.
4: Indicates that no files were uploaded.
6: Unable to find temp folder
7: File Write Failed
Name: The file name of the upload
Size: Sizes of files
Type: File type
Tmp_name: Temp File


2, determine whether to upload files is_uploaded_file ()

1<?PHP2 //determine if a file can be uploaded to the server3 //$_file[' myfile ' [' ERROR '] 0 means upload is successful4 if($_files[' MyFile '] [' ERROR '] > 0){5     Echo' Upload error ';6     Switch($_files[' MyFile '] [' Error ']){7          Case1: die(' Upload file size exceeds the contract value in PHP config file: upload_max_filesize ');8          Case2: die(' upload size exceeds the contract value in the form: Max_file_size '));9          Case3: die(' files are only partially uploaded ');Ten          Case4: die(' No files uploaded '); One         default: die(' Unknown error '); A     } - } -?>


3. Move the uploaded file to the new location Move_uploaded_file ()

1<?PHP2 $path= './uploads '3 if(Is_uploaded_file($_files[' MyFile '] [' Tmp_name '])){4     if(!Move_uploaded_file($_files[' MyFile '] [' Tmp_name '],$path. ‘/‘ .$filename)){5          die(' cannot move files to the specified directory ');6     }7 }8?>

4, determine whether the type can be uploaded

1<?PHP2 //set the allowed upload type3 $allowtype=Array(' gif ', ' jpg ', ' png ');4 //split the fractional group and remove the suffix name5 $cs=Array_pop(Explode(‘.‘,$FILES[' MyFile '] [' Name ']));6 //determines whether the type of upload is allowed by judging the file's extension7 if(!In_array($cs,$allowtype)){8      die("The suffix name is {$cs}, not a file type allowed to upload ");9 }Ten?>

5. Determine the allowable upload file size

1 <? PHP 2 $size = 100000; 3 if ($FILES$size) {4      die (' exceeds the allowable {$size} byte size '): 5 }6 ?>

6. System definition file name after uploading

1 <? PHP 2 // For system security, also for files with the same name will not be overwritten, after uploading the file name using the system definition 3 $cs Array_pop (explode('. ',$FILES[' myfile '] [' name '])); 4 $filename Date (' Ymdhis '). Rand $cs ; 5 }6 ?>

Iv. Upload Multiple files

As long as the client provides several input forms of type file, and sets a different Name property value

1 <HTML>2 <Head><title>File Upload</title></Head>3 <Body>4     <formAction= ' upload.php 'Method= ' POST 'enctype= ' Multipart/form-data '>5         <inputtype= ' hidden 'name= ' max_file_size 'value= ' 1000000 '>6Select File 1:<inputtype= ' file 'name= ' myfile[] '><BR>7Select File 2:<inputtype= ' file 'name= ' myfile[] '><BR>8Select File 3:<inputtype= ' file 'name= ' myfile[] '><BR>9         <inputtype= ' Submit 'value= ' upload file '>Ten     </form> One </Body> A </HTML>

V. File download

1<?PHP2 Header("Content-type: Type");//Specify the type of file to download3 Header("Content-disposition:attachment;filename= file name");//Specify the downloaded file description4 Header("Content-length: File Size");//Specify the file size to download5 ReadFile("./uploads/".$picname);//read and output picture content6?>

Vi. Mind Mapping

"Code Learning" php File upload and download

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.