php move_uploaded_file
时间: 2024-01-13 08:02:46 浏览: 69
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.
阅读全文