Home>Article>Backend Development> move_uploaded_file() function in PHP
This article mainly introduces the PHP move_uploaded_file() function, which actually moves the uploaded file to a new location. Friends who need it can refer to the
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)
Description | |
---|---|
Required. Specifies the files to be moved. | |
Required. Specifies the new location of the file. |
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:$fileName = $_SERVER['DOCUMENT_ROOT'].'/Basic/uploads/'.$_FILES['file']['name']; move_uploaded_file($_FILES['file']['tmp_name'],$fileName )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:
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 the 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:
move_uploaded_file() function example
Use the move_uploaded_file() function to upload files to the server."; echo "Please ensure that if safe_mode is on that the " . "UID PHP is using matches the file."; exit; } else { echo "The file has been successfully uploaded!"; } ?>
move_uploaded_file File upload failure case and solution
Today, when implementing a PHP script that uploads an avatar image file when a user registers, a problem occurred: php The script code is as follows:0) { echo "Return Code: " . $_FILES["file"]["error"] . "When I execute the above script, the script outputs "Stored failed: file save error", which is obviously an error. In the php_error_log file, I saw the error problem: insufficient permissions, I finally found what went wrong: the destination directory where we store the pictures does not have permissions for the user who executes the PHP script. The user who executes the PHP script is not the same user who wrote the script code and created the picture folder, so only You need to change the file permissions to 777.
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { if(is_uploaded_file($_FILES['file']['tmp_name'])){ $stored_path = ROOT.'/upload/'.basename($_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'],$stored_path)){ echo "Stored in: " . $stored_path; }else{ echo 'Stored failed:file save error'; } }else{ echo 'Stored failed:no post '; } } } ?>
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:超过了文件大小,在php.ini文件中设置The difference between the constructor _construct() and _initialize() of the class in ThinkPHP
How to use distinct in Thinkphp
The above is the detailed content of move_uploaded_file() function in PHP. For more information, please follow other related articles on the PHP Chinese website!