PHP Development File Upload

Source: Internet
Author: User

1. Configure the php.ini file
The following property values are primarily set:


2, pre-defined variables
The list is as follows:

Create an upload field to upload the file data via the _files variable output. The sample code is as follows:

<form Action="" enctype="Multipart/form-data" method ="POST" name="UploadFile">Upload file:<input type="file" name="Upfile" /><br> <input type="Submit" value="Upload" /></form>  <?php if (Is_uploaded_file ($_files[' Upfile '] [' Tmp_name '])) {    $upfile =$_files["Upfile"]; foreach ($upfile  as $value) { echo $value.    "<br>"; }}?> 

The results of the operation are as follows:

3, File upload function
PHP uses the Move_uploaded_file () function to upload files with the following syntax:
BOOL Move_uploaded_file (string filename,string destination)
The function uploads the file to the specified location and returns True if successful, and false if it fails. parameter filename, temporary file name, $_file[tmp_name]; parameter destination is the new path and name saved after uploading.

The sample code is as follows:

<form action=""Enctype="Multipart/form-data"Method="POST"Name="UploadFile"> Upload files: <inputtype="File"Name="Upfile"/><br> <inputtype="Submit"Value="Upload"/></form> <?phpif(Is_uploaded_file ($_files[' Upfile '][' Tmp_name '])) {$upfile=$_files["Upfile"];if(Move_uploaded_file ($upfile[' Tmp_name '],$upfile[' name '])) {Echo "Upload succeeded"; }Else{Echo "Upload failed"; }}?>

The results of the operation are as follows:

4, multi-file upload
There are many ways to upload multiple files, we have evolved a multi-file upload based on a single file. The code examples are as follows:

<form Action="index.php" enctype="Multipart/form-data" Method="POST" name="UploadFile">Upload file:<br> <input type="file" name="Upfile1" /><br><input type="file" name="Upfile2" /><br><input type="file" name="Upfile3" /><br><input type="Submit" value="Upload" /><br> </form> <?php //Header (' Content-type:text/html;charset=utf-8 ');$file 1=$_files[' Upfile1 '];$file 2=$_files[' Upfile2 '];$file 3=$_files[' Upfile3 '];if($file 1[' ERROR '] = = UPLOAD_ERR_OK) {Echo "Path:".$file 1[' name ']."<br>";if(Move_uploaded_file ($file 1[' Tmp_name '],$file 1[' name '])) {Echo "Upload success <br>"; }Else{Echo "Upload failed <br>"; }}if($file 2[' ERROR '] = = UPLOAD_ERR_OK) {Echo "Path:".$file 2[' name ']."<br>";if(Move_uploaded_file ($file 2[' Tmp_name '],$file 2[' name '])) {Echo "Upload success <br>"; }Else{Echo "Upload failed <br>"; }}if($file 3[' ERROR '] = = UPLOAD_ERR_OK) {Echo "Path:".$file 3[' name ']."<br>";if(Move_uploaded_file ($file 3[' Tmp_name '],$file 3[' name '])) {Echo "Upload success <br>"; }Else{Echo "Upload failed <br>"; }        }?>

The results of the operation are as follows:

The sample code for the second way to upload multiple files is as follows:

<form Action="index.php" enctype="Multipart/form-data"  Method="POST" name="UploadFile">Upload file:<br> <input type="file" name="upfile[]" /><br><input type="file" name="upfile[]" /><br><input type="file" name="upfile[]" /><br><input type="Submit" value="Upload" /><br> </form> <?php //Header (' Content-type:text/html;charset=utf-8 ');$filearray=$_files[' Upfile '];p Rint_r ($filearray);foreach($filearray[' ERROR '] as $key=$error){if($error==UPLOAD_ERR_OK) {Echo "file name:".$filearray[' name '][$key]."<br>";if(Move_uploaded_file ($filearray[' Tmp_name '][$key],$filearray[' name '][$key])) {Echo "Upload success <br>"; }Else{Echo "Upload failed <br>"; }    }}?>

The results of the operation are as follows:

In addition to the above two, there is a third way to upload multiple files, of course, I think the most perfect way, is to select multiple files at once to upload. The main thing is to use swfupload to achieve. Because my current knowledge is limited, I will study this together with everyone in the back.

PHP Development File Upload

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.