What is the recommended method for handling file uploads in PHP?
When handling file uploads in PHP, it is recommended to use the $_FILES superglobal array to access the uploaded file data. To ensure security, always validate the file type and size before processing the upload. Move the uploaded file to a secure location on the server using move_uploaded_file() function.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$file = $_FILES['file'];
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $uploadFile)) {
echo "File uploaded successfully!";
} else {
echo "Error uploading file.";
}
}
?>