What are the best practices for ensuring proper file upload functionality in PHP without relying on register_globals being turned on?
When register_globals is turned off in PHP, it is important to use superglobal arrays like $_FILES to handle file uploads safely. To ensure proper file upload functionality without relying on register_globals, always validate file uploads, check for errors, move uploaded files to a secure location, and set appropriate permissions.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
if ($file['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($file['name']);
if (move_uploaded_file($file['tmp_name'], $uploadFile)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}
} else {
echo "Error: " . $file['error'];
}
} else {
echo "No file uploaded.";
}
}
?>