What is the purpose of the move_uploaded_file function in PHP and how is it typically used?
The move_uploaded_file function in PHP is used to move an uploaded file to a new location on the server. This function is typically used after a file has been uploaded via a form submission and allows the file to be securely saved to a specified directory on the server.
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
$uploadedFile = $_FILES['file']['tmp_name'];
$destination = 'uploads/' . $_FILES['file']['name'];
if (move_uploaded_file($uploadedFile, $destination)) {
echo 'File uploaded successfully!';
} else {
echo 'Error moving file.';
}
} else {
echo 'Error uploading file.';
}
Related Questions
- What are some common reasons why a PHP SQL query may not produce any output?
- How can one efficiently handle cases where a key is missing in one array when trying to match and extract values in PHP?
- How can PHP developers ensure that specific file types are allowed for upload while avoiding exclusion due to MIME type detection problems?