Php Development file Upload
1. configure the php. ini file
Set the following attribute values:
2. pre-defined variables
The list is as follows:
Create an upload domain and output the uploaded file information through the _ FILES variable. The sample code is as follows:
"; } } ?>
The running result is as follows:
3. file upload function
Use the move_uploaded_file () function in php to upload files. The syntax is as follows:
Bool move_uploaded_file (string filename, string destination)
This function uploads a file to a specified position. if the file is successfully uploaded, true is returned. if the file fails, false is returned. The parameter filename is the temporary FILE name, that is, $ _ FILE [tmp_name]. the destination parameter is the new path and name saved after Upload.
The sample code is as follows:
The running result is as follows:
4. multifile Upload
There are multiple methods for uploading multiple files. next we will evolve a multifile Upload method based on a single file upload. The sample code is as follows:
"; If (move_uploaded_file ($ file1 ['tmp _ name'], $ file1 ['name']) {echo" uploaded successfully
";} Else {echo" Upload failed
";}} If ($ file2 ['error'] = UPLOAD_ERR_ OK) {echo" path: ". $ file2 ['name']."
"; If (move_uploaded_file ($ file2 ['tmp _ name'], $ file2 ['name']) {echo" uploaded successfully
";} Else {echo" Upload failed
";}} If ($ file3 ['error'] = UPLOAD_ERR_ OK) {echo" path: ". $ file3 ['name']."
"; If (move_uploaded_file ($ file3 ['tmp _ name'], $ file3 ['name']) {echo" uploaded successfully
";} Else {echo" Upload failed
";}}?>
The running result is as follows:
The sample code for the second multi-file Upload method is as follows:
$ Error) {if ($ error = UPLOAD_ERR_ OK) {echo "File name:". $ filearray ['name'] [$ key]."
"; If (move_uploaded_file ($ filearray ['tmp _ name'] [$ key], $ filearray ['name'] [$ key]) {echo" uploaded successfully
";} Else {echo" Upload failed
";}}?>
The running result is as follows:
In addition to the above two methods, there is also a third method for uploading multiple files. of course, I think the most perfect method is to select multiple files at a time for uploading. It is mainly implemented using swfupload. Due to my limited current knowledge, I will learn this with you later.