This time I will bring you a detailed explanation of the practical case of the PHP move_uploaded_file() function. What are the precautions when using the PHP move_uploaded_file() function. The following is a practical case, let's take a look.
Definition and Usage
move_uploaded_file()
Function moves the uploaded file to a new location.
If successful, return true, otherwise return false.
Syntax
move_uploaded_file(file,newloc)
##ParametersDescriptionfileRequired. Specifies the files to be moved. newlocRequired. Specifies the new location of the file.Description
This function checks and ensures that the file specified by file is a legal upload file (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by newloc.
If file is not a legal uploaded file, no operation will occur and move_uploaded_file() will return false.
If file is a legal uploaded file but cannot be moved for some reason, no action will occur and move_uploaded_file() will return false and a warning will be issued.
This kind of check is particularly important if the uploaded file may cause its content to be displayed to the user or other users of this system.
Tips and Notes
Note: This function is only used for files uploaded via HTTP POST.
Note: If the target file already exists, it will be overwritten.
Security Supplement
Introduction from w3c, let’s talk about the problems I encountered.
Generally speaking, we will write the save file like this:
1 2 |
|
First explain, the meaning of these two codes: save the file directly, and the file name is also the file name uploaded by the user
Okay, now the risk is here:
①Save the file directly.
This means that the file will not be identified in any way. If a user uploads a piece of background code and saves it as a jpg suffix or other, if the administrator accidentally maps it to php and then accesses the background, - - Result It is conceivable that if he deletes all databases in the background, the entire website will directly GG. In short, saving files directly is very risky.
②Use the same file name as the user file name.
The above code will report an error if the user uses a Chinese file name.
As soon as the file name is involved, encoding is involved. If the file name is an English number, it is fine. If it contains Chinese characters, it will be a big problem and it must be re-encoded.
I think reliable storage should be like this:
①The files uploaded by users must be identified.
File recognition, this part has many functions. I think it is good to use MIME type, which is also difficult to forge.
② Change the file name.
I think it’s best to change the file name to a time format like “201803264104421”, or you can also correspond the file name to the database.
Supplement:
There are two parameters. The first parameter is the temporary file name after you upload it, which is automatically generated by the system. Usually its style is:
$_FILE["file"]["tmp_name"];
where file is the name of your front-end file upload form.
The second parameter is the new file name containing the path. For example:
"upload/1.jpg";
In this way, the file you uploaded will be moved to the subdirectory named upload in the current directory, and the file name will be saved as :1.jpg.
move_uploaded_file() function example
Use the move_uploaded_file() function to upload files to the server.
1 2 3 4 5 6 7 8 9 10 |
|
move_uploaded_file File upload failure case and solution
Today when implementing a PHP script to upload an avatar image file when user registration , a problem occurred: the php script code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
When I execute the above script, the script outputs "Stored failed: file save error", which is obviously an error. In php_error_log I saw an error in the file: insufficient permissions. I finally found the error: the destination directory where we store the images does not have permissions for the user who executes PHP. The user who executes the PHP script and I write the script code, The user who created the picture folder is not the same user, so you only need to change the file permissions to 777.
PHP Development Learning File Upload (move_uploaded_file)
Function: Move the uploaded temporary file to the upload directory. Upload has been created in the root directory! ! !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
Execution result:
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
thinkPHP5 framework detailed explanation of the steps to implement paging query
Laravel ORM detailed explanation of the Model::find cache method
The above is the detailed content of Detailed explanation of practical cases of PHP move_uploaded_file() function. For more information, please follow other related articles on the PHP Chinese website!