PHP File Uploading

PHP

PHP File Uploading यूजर को PHP स्क्रिप्ट और एक HTML form के कॉम्बिनेशन के सहायता से, सर्वर पर single और multiple फ़ाइलों को अपलोड करने में मदद करती है। एक यूजर विभिन्न प्रकार के फ़ाइल जैसे images, videos, ZIP files, PDFs और कई अन्य प्रकार के फ़ाइलो को अपलोड कर सकता है।

Creating an HTML form

निम्नलिखित उदाहरण में एक simple और आसान HTML form बनाया गया है जिसका उपयोग फ़ाइलो को अपलोड करने के लिए किया जा सकता है।

HTML form for PHP file uploading
<html>

<body>
    <form action="file-uploader.php" method="post" enctype="multipart/form-data">
        <h2>Upload File Example</h2>
        <input type="file" name="photo" id="fileSelect">
        <input type="submit" name="submit" value="Upload File">
        <p>
            <strong>Format:</strong> Only .jpg, .jpeg, .gif, .png allowed.<br>
            <strong>Size:</strong> Max size of 5 MB allowed.
        </p>
    </form>
</body>

</html>

Processing the PHP file upload

अब एक PHP फ़ाइल बनाकर PHP scripts को लिखते हैं, जिसका नाम file-uploader.php है और इसके लिए नीचे दिए गए कोड का इस्तेमाल करेंगे फ़ाइल में।

यह अपलोड की गई फ़ाइल को आपके local drive "F:/Example/Upload/" folder में स्टोर करेगा और साथ ही कुछ बुनियादी security check जैसे की फ़ाइल की प्रकार और फ़ाइल की साइज़ को लागू करेगा ताकि यह सुनिश्चित किया जा सके कि यूजर सही प्रकार के फाइल और allowed limit के भीतर ही अपलोड करें।

file-uploader.php
<?php

// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Check if file was uploaded without errors
    if (isset($_FILES["photo"])) {
        $errors = array();
        $filename = $_FILES["photo"]["name"];
        $filetype = $_FILES["photo"]["type"];
        $filesize = $_FILES["photo"]["size"];
        $file_tmp = $_FILES["photo"]["tmp_name"];
        $extensions = array("jpg", "jpeg", "gif", "png");
        $file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

        // Verify file extension
        if (in_array($file_ext, $extensions) == false) {
            $errors[] = "Error: Please select a valid file format.";
        }

        // Verify file size - 5MB maximum
        $maxsize = 5 * 1024 * 1024;
        if ($filesize > $maxsize) {
            $errors[] = "Error: File size is larger than the allowed limit.";
        }

        // Verify MYME type of the file
        if (empty($errors) == true) {
            if (file_exists("F:/Example/Upload/" . $filename)) {
                echo $filename . " already exists.";
            } else {
                move_uploaded_file($file_tmp, "F:/Example/Upload/" . $filename);
                echo "File uploaded successfully";
            }
        }
    }
}
?>

PHP $_FILES

PHP में $_FILES नामक एक global variable होता है। $_FILES में फ़ाइल से जुड़ी सभी जानकारी होती है।

यदि अपलोडिंग फॉर्म में इनपुट के नाम को सौंपा गया वैल्यू एक फ़ाइल होता है, तो PHP निम्नलिखित पांच variables बनाएगी।

PHP $_FILES Description
$_FILES[“file”][“name”] The actual name of the uploaded file.
$_FILES[“file”][“type”] The MIME type of the uploaded file.
$_FILES[“file”][“size”] The size in bytes of the uploaded file.
$_FILES[“file”][“error”] The error code associated with this file upload.
$_FILES[“file”][“tmp_name”] The PHP file uploaded in the temporary directory.
Article By: Brajlal Prasad
Created on: 17 Feb 2023  1420  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]