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:
<Form action = "" enctype = "multipart/form-data" method = "post" name = "uploadfile"> upload a 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 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:
<Form action = "" enctype = "multipart/form-data" method = "post" name = "uploadfile"> upload a 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"]; if (move_uploaded_file ($ upfile ['tmp _ name'], $ upfile ['name']) {echo "uploaded successfully ";} else {echo "Upload Failed" ;}}?>
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:
<Form action = "index. php "enctype =" multipart/form-data "method =" post "name =" uploadfile "> upload files: <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 '); $ file1 = $ _ FILES ['upfile1']; $ file2 = $ _ FILES ['upfile2']; $ file3 = $ _ FILES ['upfile3']; if ($ file1 ['error'] = UPLOAD_ERR_ OK) {echo "Path :". $ file1 ['name']. "<br>"; if (move_uploaded_file ($ file1 ['tmp _ name'], $ file1 ['name']) {echo "uploaded successfully <br> ";} else {echo "Upload Failed <br>" ;}} if ($ file2 ['error'] == UPLOAD_ERR_ OK) {echo "Path :". $ file2 ['name']. "<br>"; if (move_upload Ed_file ($ file2 ['tmp _ name'], $ file2 ['name']) {echo "uploaded successfully <br> ";} else {echo "Upload Failed <br>" ;}} if ($ file3 ['error'] == UPLOAD_ERR_ OK) {echo "Path :". $ file3 ['name']. "<br>"; if (move_uploaded_file ($ file3 ['tmp _ name'], $ file3 ['name']) {echo "uploaded successfully <br> ";} else {echo "Upload Failed <br>" ;}}?>
The running result is as follows:
The sample code for the second multi-File Upload method is as follows:
<Form action = "index. php "enctype =" multipart/form-data "method =" post "name =" uploadfile "> upload files: <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']; print_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 successful <br>" ;}else {echo "Upload Failed <br>" ;}}}?>
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.