Issue uploading zip pdf file using Php

I am a teacher, not a coder so I try to implement learning/drawing help from the internet. I am trying to receive student work through dynamic pdf using Callback that I found a very reliable code here that works - https://codepen.io/practicalPDF/pen/eYBOpEm

However when I copied a PHP code and created upload.php it did not work, can anyone please help what changes I need to make here to make it work with the callback?

<?php
/* Simple script to upload a zip file to the webserver and have it unzipped
 */

function rmdir_recursive($dir) {
    foreach (scandir($dir) as $file) {
        if ('.' === $file || '..' === $file)
            continue;
        if (is_dir("$dir/$file"))
            rmdir_recursive("$dir/$file");
        else
            unlink("$dir/$file");
    }
    rmdir($dir);
}

if ($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];

    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach ($accepted_types as $mime_type) {
        if ($mime_type == $type) {
            $okay = true;
            break;
        }
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if (!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }

    /* PHP current path */
    $path = dirname(__FILE__) . '/';  // absolute path to the directory where zipper.php is in
    $filenoext = basename($filename, '.zip');  // absolute path to the directory where zipper.php is in (lowercase)
    $filenoext = basename($filenoext, '.ZIP');  // absolute path to the directory where zipper.php is in (when uppercase)

    $targetdir = $path . $filenoext; // target directory
    $targetzip = $path . $filename; // target zip file

    /* create directory if not exists, otherwise overwrite */
    /* target directory is same as filename without extension */

    if (is_dir($targetdir))
        rmdir_recursive($targetdir);
    mkdir($targetdir, 0777);

    /* here it is really happening */

    if (move_uploaded_file($source, $targetzip)) {
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);  // open the zip file to extract
        if ($x === true) {
            $zip->extractTo($targetdir); // place in the directory with same name  
            $zip->close();

            unlink($targetzip);
        }
        $message = "Your .zip file was uploaded and unpacked.";
    } else {
        $message = "There was a problem with the upload. Please try again.";
    }
}
?>

Telling us that something didn’t work is pointless. We are not there with you and don’t know what occurred that leads you to believe that it didn’t work. Something occurred, even if it was just a blank page or the page just appeared to reload.

Next, this upload handling code might work under perfect conditions, but it has no useful error handling or validation logic to get it to tell you if and why it failed. When dealing with user submitted data, almost every if() conditional test needs an else branch so that the code does ‘something’ when the condition being tested failed.

Post method form processing code should -

  1. Detect if a post method form was submitted before referencing any of the form data.
  2. If the total size of the submitted form data is greater than the post_max_size setting on the server, the form submission will be discarded and both the $_POST and $_FILES arrays will be empty. You must detect this condition and setup a message for the user telling them that the form data was too large and could not be processed. Since the code is only using $_FILES data, you would need to test if $_FILES is empty to detect this.
  3. After you have determined that there is data in $_FILES, you need to test the [‘error’] element. If there are no errors, you can use the rest of the data in $_FILES. The current code is testing if the [‘name’] element is true, however, there are some errors where the name contains a value but the upload failed. For errors (there’s a list in the php.net documentation) that the user has control over, you would setup a message telling them what was wrong with the data that was submitted. For the (internal) errors that the user cannot do anything about, you would set up general failure message for the user and log all the information about the actual error.
  4. If the upload was successful, you would then validate the uploaded file information. Note: all external data can be set to anything and cannot be trusted. The mime type from the [‘type’] element should not be used and different browsers and versions of browsers also set it to different values for the same type of file. Whenever possible, detect the file type internally using php functions.
  5. Any user/validation messages should be added to an array. After the upload error handling and validation, if there are no errors (the array will be empty), process the submitted form data. If there are errors, display them when you re-display the form. For any additional errors that occur when processing the form data, add them to the errors array.
  6. After the end of all the form processing code, if there are no errors, redirect to the exact same url of the current page to cause a get request for the page. This will prevent the browser from trying to re-submit the form data if the user reloads the page or browses back to the url of the page.
Sponsor our Newsletter | Privacy Policy | Terms of Service