// Enable error reporting for debuggingerror_reporting(E_ALL);ini_set('display_errors', 1);// Set the target directory for uploads$targetDirectory = "uploads/";// Create the target directory if it doesn't existif (!is_dir($targetDirectory)) { mkdir($targetDirectory, 0777, true);}// Check if a file was uploadedif ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['fileToUpload'])) { $file = $_FILES['fileToUpload']; // Check for file upload errors and show the error code if ($file['error'] != 0) { echo "File upload error: " . $file['error']; exit; } // Get file details $fileName = basename($file['name']); $targetFilePath = $targetDirectory . $fileName; $fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); // Allowed file types (now includes php for this example, but risky) $allowedTypes = ['jpg', 'png', 'jpeg', 'gif', 'pdf', 'php']; if (!in_array($fileType, $allowedTypes)) { echo "Sorry, only JPG, JPEG, PNG, GIF, PDF, and PHP files are allowed."; exit; } // Check file size (5MB max) $maxFileSize = 5 * 1024 * 1024; // 5MB if ($file['size'] > $maxFileSize) { echo "Sorry, your file is too large."; exit; } // Ensure the target directory is writable if (!is_writable($targetDirectory)) { echo "Sorry, the upload directory is not writable."; exit; } // Try to move the uploaded file to the target directory if (move_uploaded_file($file['tmp_name'], $targetFilePath)) { echo "The file " . htmlspecialchars($fileName) . " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; }}<!-- HTML Form for file upload --><!DOCTYPE html><html lang="en">
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">