What are the potential pitfalls of using relative paths instead of absolute paths when handling file uploads in PHP?
Using relative paths when handling file uploads in PHP can lead to potential pitfalls such as security vulnerabilities and inconsistent file paths. It is recommended to use absolute paths to ensure that files are uploaded to the correct directory and to prevent unauthorized access to sensitive files on the server.
// Using absolute path for file uploads
$uploadDir = '/var/www/html/uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
echo "File is valid, and was successfully uploaded.";
} else {
echo "Possible file upload attack!";
}