php files

 here i have discussed about how to upload files inside php, unique unique hash-name each-time.

SourceURL:file://Document1

index.html

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

                <div class="mb-3">

                  <label for="exampleInputEmail1" class="form-label">Email address</label>

                  <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">

                  <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>

                </div>

                <div class="mb-3">

                    <label for="formFile" class="form-label">Default file input example</label>

                    <input class="form-control" type="file" name="formFile" id="formFile">

                  </div>

                <button type="submit" class="btn btn-primary">Submit</button>

              </form>

 

upload.php

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

    // Ensure the uploads directory exists

    $target_dir = "uploads/";

    if (!is_dir($target_dir)) {

        mkdir($target_dir, 0777, true);

    }

 

    // Generate a unique hash for the file

    $file_temp = $_FILES["formFile"]["tmp_name"];

    $file_name = basename($_FILES["formFile"]["name"]);

    $unique_id = uniqid(); // Generate a unique ID based on the current time in microseconds

    $file_hash = hash('md5', file_get_contents($file_temp) . $unique_id); // Include the unique ID in the hash

    $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

    $target_file = $target_dir . $file_hash . '.' . $file_ext;

    $uploadOk = 1;

 

    // Check if file already exists

    if (file_exists($target_file)) {

        echo "Sorry, file already exists.";

        $uploadOk = 0;

    }

 

    // Check if $uploadOk is set to 0 by an error

    if ($uploadOk == 0) {

        echo "Sorry, your file was not uploaded.";

    } else {

        if (move_uploaded_file($_FILES["formFile"]["tmp_name"], $target_file)) {

            // echo "The file ". htmlspecialchars(basename($_FILES["formFile"]["name"])) . " has been uploaded.";

            echo "The file has been uploaded as " . htmlspecialchars($target_file) . ".";

            echo "<br/>";

            echo getcwd();

        } else {

            echo "Sorry, there was an error uploading your file.";

        }

    }

}

?>

 

** for bulk upload

index.html

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

                <div class="mb-3">

                    <label for="formFile" class="form-label">Default file input example</label>

                    <input class="form-control" type="file" name="formFile" id="formFile">

                </div>

                <div class="mb-3">

                    <label for="formFile" class="form-label">Default file input example</label>

                    <input class="form-control" type="file" name="formFile2" id="formFile2">

                </div>

                <div class="mb-3">

                    <label for="formFile" class="form-label">Default file input example</label>

                    <input class="form-control" type="file" name="formFile3" id="formFile3">

                </div>

                <button type="submit" class="btn btn-primary">Submit</button>

            </form>

 

bulkupload.php

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

    // Ensure the uploads directory exists

    $target_dir = "uploads/";

    if (!is_dir($target_dir)) {

        mkdir($target_dir, 0777, true);

    }

 

    /** formfile1 - first file upload */

 

    // Generate a unique hash for the file

    $file_temp = $_FILES["formFile"]["tmp_name"];

    $file_name = basename($_FILES["formFile"]["name"]);

    $unique_id = uniqid(); // Generate a unique ID based on the current time in microseconds

    $file_hash = hash('md5', file_get_contents($file_temp) . $unique_id); // Include the unique ID in the hash

    $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

    $target_file = $target_dir . $file_hash . '.' . $file_ext;

    $uploadOk = 1;

 

    // Check if file already exists

    if (file_exists($target_file)) {

        echo "Sorry, file already exists.";

        $uploadOk = 0;

    }

 

    // Check if $uploadOk is set to 0 by an error

    if ($uploadOk == 0) {

        echo "Sorry, your file was not uploaded.";

    } else {

        if (move_uploaded_file($_FILES["formFile"]["tmp_name"], $target_file)) {

            // echo "The file ". htmlspecialchars(basename($_FILES["formFile"]["name"])) . " has been uploaded.";

            echo "The file has been uploaded as " . htmlspecialchars($target_file);

            echo "<br/>";

            echo getcwd();

        } else {

            echo "Sorry, there was an error uploading your file.";

        }

    }

 

    /** second file upload - formfile 2 */

     // Generate a unique hash for the file

     $file_temp = $_FILES["formFile2"]["tmp_name"];

     $file_name = basename($_FILES["formFile2"]["name"]);

     $unique_id = uniqid(); // Generate a unique ID based on the current time in microseconds

     $file_hash = hash('md5', file_get_contents($file_temp) . $unique_id); // Include the unique ID in the hash

     $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

     $target_file = $target_dir . $file_hash . '.' . $file_ext;

     $uploadOk = 1;

 

     // Check if file already exists

     if (file_exists($target_file)) {

         echo "Sorry, file already exists.";

         $uploadOk = 0;

     }

 

     // Check if $uploadOk is set to 0 by an error

     if ($uploadOk == 0) {

         echo "Sorry, your file was not uploaded.";

     } else {

         if (move_uploaded_file($_FILES["formFile2"]["tmp_name"], $target_file)) {

             // echo "The file ". htmlspecialchars(basename($_FILES["formFile"]["name"])) . " has been uploaded.";

             echo "The file has been uploaded as " . htmlspecialchars($target_file);

             echo "<br/>";

             echo getcwd();

         } else {

             echo "Sorry, there was an error uploading your file.";

         }

     }

 

      /** third file upload - formfile 3 */

     // Generate a unique hash for the file

     $file_temp = $_FILES["formFile3"]["tmp_name"];

     $file_name = basename($_FILES["formFile3"]["name"]);

     $unique_id = uniqid(); // Generate a unique ID based on the current time in microseconds

     $file_hash = hash('md5', file_get_contents($file_temp) . $unique_id); // Include the unique ID in the hash

     $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

     $target_file = $target_dir . $file_hash . '.' . $file_ext;

     $uploadOk = 1;

 

     // Check if file already exists

     if (file_exists($target_file)) {

         echo "Sorry, file already exists.";

         $uploadOk = 0;

     }

 

     // Check if $uploadOk is set to 0 by an error

     if ($uploadOk == 0) {

         echo "Sorry, your file was not uploaded.";

     } else {

         if (move_uploaded_file($_FILES["formFile3"]["tmp_name"], $target_file)) {

             // echo "The file ". htmlspecialchars(basename($_FILES["formFile"]["name"])) . " has been uploaded.";

             echo "The file has been uploaded as " . htmlspecialchars($target_file);

             echo "<br/>";

             echo getcwd();

         } else {

             echo "Sorry, there was an error uploading your file.";

         }

     }

 

 

}

?>

 

Popular posts from this blog

MCSL 216 MCA NEW Practical ~ common questions suggestions

STRAPI

jitsi