Php file Upload introduction? For instructions on how to handle these problems in chapter 39th of the PHP Manual, copy and paste the following? 39? Chapter php file Upload
?
For instructions on how to handle these problems in chapter 39th of the PHP manual, I copy and paste the following:
--------------------------------------------
Number? 39? Chapter? File upload processing
Directory
POST? Upload method
Error message description
Common defects
Upload multiple files
Right? PUT? Method support
POST? Upload method
This feature allows users to upload text and binary files. Use? PHP? Authentication and file operation functions can fully control who are allowed to upload files and how to handle them after the files are uploaded .?
PHP? Can accept any conformity? RFC-1867? Standard browsers (including? Netscape? Navigator? 3? And later versions, patched? Microsoft? Internet? Explorer? 3? Or a later version .?
Related settings :? See? Php. ini? ? File_uploads, upload_max_filesize, upload_tmp_dirpost_max_size? And? Max_input_time? Set options .?
Please note? PHP? Also supported? PUT? Method file upload, Netscape? Composer? And? W3C? ? Amaya? The client uses this method. See? PUT? To obtain more information .?
Example? 39.1 .? File upload form
You can create a special form as follows to support file upload :?
????
????
In the above example? _ URL __? Should be replaced, pointing to a real? PHP? File .?
MAX_FILE_SIZE? The hidden field (in bytes) must be placed before the input field of the file, and its value is the maximum size of the received file. This is a suggestion for the browser, PHP? This item is also checked. This setting can be simply bypassed on the browser side, so do not expect this feature to block large files. Actually, PHP? The maximum value of the uploaded files in the settings will not expire. But it is best to add this project to the form, because it can avoid the trouble of file Upload failure only after the user spends the time waiting to upload a large file .?
Note :? What are the attributes of the file upload form? Enctype = "multipart/form-data", otherwise the file cannot be uploaded .?
Global variable? $ _ FILES? ? PHP? 4.1.0? (Used in earlier versions? $ HTTP_POST_FILES? ). This array contains information about all uploaded files .?
In the above example? $ _ FILES? The contents of the array are as follows. Assume that the name of the file upload field is as follows? Userfile. The name can be arbitrarily named .?
$ _ FILES ['userfile'] ['name']
The original name of the client machine File .?
$ _ FILES ['userfile'] ['type']
File? MIME? Type, if the browser provides this information. An example is "image/gif ". But here? MIME? Type in? PHP? Does not check, so do not take this value for granted .?
$ _ FILES ['userfile'] ['size']
Size of the uploaded file, in bytes .?
$ _ FILES ['userfile'] ['tmp _ name']
Temporary file name stored on the server after the file is uploaded .?
$ _ FILES ['userfile'] ['error']
The error code related to the file upload. This project is in? PHP? 4.2.0? Added in version .?
After a file is uploaded, it is stored in the default temporary directory of the server by default, unless? Php. ini? In? Upload_tmp_dir? Set it to another path. Can the default temporary directory of the server be changed? PHP? What are the environment variables of the runtime environment? TMPDIR? To reset, but in? PHP? Does the script run internally? Putenv ()? Function settings do not work. This environment variable can also be used to confirm that other operations are also performed on the uploaded file .?
Example? 39.2 .? Make file Upload take effect
Please refer to the function? Is_uploaded_file ()? And? Move_uploaded_file ()? For further information. The following example processes the file upload provided by the form .?
//? In? PHP? Versions? Earlier? Than? 4.1.0 ,? $ HTTP_POST_FILES? Shocould? Be? Used? Instead
//? Of? $ _ FILES.
$ Uploaddir? =? '/Var/www/uploads /';
$ Uploadfile? =? $ Uploaddir ?.? Basename ($ _ FILES ['userfile'] ['name']);
Echo? '
';
if?(move_uploaded_file($_FILES['userfile']['tmp_name'],?$uploadfile))?{
????echo?"File?is?valid,?and?was?successfully?uploaded.\n";
}?else?{
????echo?"Possible?file?upload?attack!\n";
}
echo?'Here?is?some?more?debugging?info:';
print_r($_FILES);
print?"
";
?>?
Which of the following files can be uploaded? PHP? To determine the operations to be performed on the file, the script should perform any logical check. For example, can I use? $ _ FILES ['userfile'] ['size']? Variables can be used to exclude files that are too large or too small? $ _ FILES ['userfile'] ['type']? Variable to exclude files that do not conform to a certain standard, but only this is the first step in a series of checks, because this value is completely controlled by the client? PHP? Does not check. ? PHP? 4.2.0? Start, you can also pass? $ _ FILES ['userfile'] ['error']? Variable to plan the next step based on different error codes. In any case, you can either delete the file from the temporary directory or move it to another place .?
If no file is selected for upload in the form, then? PHP? Variable? $ _ FILES ['userfile'] ['size']? The value is? 0, $ _ FILES ['userfile'] ['tmp _ name']? Will be empty .?
If the file is not moved to other places or renamed, the file will be deleted at the end of the Form Request .?
Example? 39.3 .? Upload a group of files
PHP? ? HTML? The array feature even supports file types .?
???
Foreach? ($ _ FILES ["pictures"] ["error"]? As? $ Key? =>? $ Error )? {
???? If? ($ Error? ==? UPLOAD_ERR_ OK )? {
???????? $ Tmp_name? =? $ _ FILES ["pictures"] ["tmp_name"] [$ key];
???????? $ Name? =? $ _ FILES ["pictures"] ["name"] [$ key];
???????? Move_uploaded_file ($ tmp_name ,? "Data/$ name ");
????}
}
?>
?