Upload file

 

fileloader.html

<!DOCTYPE html>

<html>

<head>

    <title>File Upload</title>

</head>

<body>

    <h2>Upload Image File (GIF, JPEG, PJPEG, < 20KB)</h2>

    <form action="upload_file.php" method="post" enctype="multipart/form-data">

        <input type="file" name="image">

        <br><br>

        <input type="submit" name="submit" value="Upload">

    </form>

</body>

</html>



upload_file.php


<?php

if (isset($_POST['submit'])) {

    $file = $_FILES['image'];


    // Get file properties

    $fileName = $file['name'];

    $fileTmp = $file['tmp_name'];

    $fileSize = $file['size'];

    $fileType = $file['type'];


    // Allowed MIME types

    $allowedTypes = ['image/gif', 'image/jpeg', 'image/pjpeg'];


    if (in_array($fileType, $allowedTypes) && $fileSize < 20000) {

        $uploadDir = "uploads/";

        if (!is_dir($uploadDir)) {

            mkdir($uploadDir); // Create upload folder if it doesn't exist

        }


        $destination = $uploadDir . basename($fileName);

        if (move_uploaded_file($fileTmp, $destination)) {

            echo "File uploaded successfully: " . $fileName;

        } else {

            echo "Failed to upload file.";

        }

    } else {

        echo "Invalid file type or file size too large. Only GIF, JPEG, PJPEG under 20KB allowed.";

    }

}

?>


Comments