The move_uploaded_file() function is used to move the uploaded file to a new location. The syntax is move_uploaded_file(file,newloc). If file is not a legal uploaded file, no operation will occur and false will be returned.
How to use the php move_uploaded_file() function?
Function: Move the uploaded file to a new location; if the target file already exists, it will be overwritten.
Syntax:
move_uploaded_file(file,newloc)
Parameters:
● file: required. Specifies the files to be moved.
● newloc: required. Specifies the new location of the file.
Return value: The function returns TRUE if successful, and FALSE if failed.
Note:
1. This function is only used for files uploaded through HTTP POST.
2. 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 legitimate 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.
php move_uploaded_file() function usage example
The following example creates an upload form that allows uploading files with a file size of less than 1MB. The specific sample code is as follows:
<html> <body> <!--上传表单,有一个上传文件域--> <form method="post" action="" enctype="multipart/form-data" name="form"> <input type="file" name="up_file"> <!-- 提交按钮--> <input type="submit" name="submit" value="上传"> </form> </body> </html> <?php header("Content-Type:text/html; charset=utf-8"); if(!empty($_FILES['up_file']['name'])){ //判断是否有文件 $fileinfo = $_FILES['up_file']; //将文件信息赋给变量$fileinfo if($fileinfo['size']<1000000 && $fileinfo['size']>0){ //判断文件大小 move_uploaded_file($fileinfo['tmp_name'],$fileinfo['name']); //上传文件 echo "上传成功"; }else{ echo '文件太大或未知'; } } ?>
Select a file to upload, the output result is as follows:
Instructions:
Use the move_uploaded_file() function To upload a file, when creating a form, you must set the form form's enctype="multipart/form-data"
.
The above is the detailed content of How to use php move_uploaded_file function. For more information, please follow other related articles on the PHP Chinese website!