Basic File Upload in PHP


Handle a simple file upload in a PHP script.

Source Code

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['fileToUpload'])) {
    $targetDir = "uploads/";
    $targetFile = $targetDir . basename($_FILES['fileToUpload']['name']);
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $targetFile)) {
        echo "The file " . htmlspecialchars(basename($_FILES['fileToUpload']['name'])) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
Note: Always ensure proper validation and sanitation of uploaded files to prevent security risks.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments