What is the correct function to use for uploading files in PHP?
To upload files in PHP, you should use the move_uploaded_file() function. This function moves an uploaded file to a new location on the server. It takes two parameters: the temporary location of the uploaded file and the destination where the file should be moved to. Make sure to use this function after validating the file and checking for any errors during the upload process.
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
$tempFile = $_FILES['file']['tmp_name'];
$destination = 'uploads/' . $_FILES['file']['name'];
if (move_uploaded_file($tempFile, $destination)) {
echo 'File uploaded successfully.';
} else {
echo 'Error uploading file.';
}
} else {
echo 'Error during file upload.';
}
Keywords
Related Questions
- What precautions should be taken when changing the script meta encoding from ISO to UTF in PHP to prevent data corruption or display issues?
- Are there any security considerations to keep in mind when implementing htaccess rules for URL redirection in PHP?
- How can PHP developers effectively utilize jQuery selectors to interact with and modify HTML elements in a dynamic web application?