move_upload_file
时间: 2023-10-16 22:06:37 浏览: 43
`move_uploaded_file`是PHP中的一个函数,用于将上传的文件移动到指定位置。
语法如下:
```
bool move_uploaded_file ( string $filename , string $destination )
```
其中,`$filename`是上传文件的临时路径,`$destination`是文件移动后的目标路径。
该函数会返回一个布尔值,表示文件是否移动成功。如果成功,返回`true`,否则返回`false`。
相关问题
php move_uploaded_file
The move_uploaded_file function in PHP is used to move an uploaded file to a new location on the server. The function takes two parameters: the path to the temporary uploaded file and the path to the new location where the file should be moved.
Syntax:
move_uploaded_file($temporaryFilePath, $newFilePath);
Example:
If you have a form for uploading a file, you can use the move_uploaded_file function to move the uploaded file to a new directory.
HTML form:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
PHP script:
if(isset($_FILES['file']['name'])) {
$tempFilePath = $_FILES['file']['tmp_name'];
$newFilePath = 'uploads/' . $_FILES['file']['name'];
if(move_uploaded_file($tempFilePath, $newFilePath)) {
echo "File uploaded successfully!";
} else {
echo "Error uploading file.";
}
}
In this example, the uploaded file is moved from its temporary location to a new directory called "uploads" with its original name. The function returns true if the file is moved successfully, and false if there is an error.
if (isset($_POST['submit'])) { if (file_exists($UPLOAD_ADDR)) { if (($_FILES['upload_file']['type'] == 'image/jpeg') || ($_FILES['upload_file']['type'] == 'image/png') || ($_FILES['upload_file']['type'] == 'image/gif')) { if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $UPLOAD_ADDR . '/' . $_FILES['upload_file']['name'])) { $img_path = $UPLOAD_ADDR . $_FILES['upload_file']['name']; $is_upload = true; } } else { $msg = '文件类型不正确,请重新上传!'; } } else { $msg = $UPLOAD_ADDR.'文件夹不存在,请手工创建!'; } }
这段代码是一个 PHP 文件上传的逻辑。首先,它通过检查是否有名为 "submit" 的 POST 参数来判断是否有文件提交。接下来,它检查是否存在名为 $UPLOAD_ADDR 的文件夹。如果文件夹存在,则继续执行。
然后,它检查上传的文件类型是否为 JPEG、PNG 或 GIF 图像类型。如果是其中之一,则将上传的文件移动到 $UPLOAD_ADDR 文件夹下,并将文件路径保存到 $img_path 变量中,并设置 $is_upload 为 true。
如果文件类型不是这三种图像类型之一,则将错误信息保存到 $msg 变量中。
如果 $UPLOAD_ADDR 文件夹不存在,则将错误信息保存到 $msg 变量中。
这段代码的功能是接收用户上传的图像文件,并将其保存到指定的文件夹中。如果文件类型不正确或文件夹不存在,会返回相应的错误信息。