1

So I'm creating a temporary folder for my extracted ZIP files in server. I tested my code in PHP 7 but unfortunately I needed to run this code in PHP 5.6.25 and system.php doesn't work on php 5 I dont know why.

I needed to created a temporary folder and delete it once the code is done, so I made this.

<?php
require_once "System.php";     
$tempdir  = System::mktemp("-d prefix");
echo $tempdir;
// returns: /tmp/8e9MLi
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' || strtolower($name[1]) == 'xlsx' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip or .csv file. Please try again.";
    }

    $target_path = $tempdir."\\".$filename;  // change this to the correct site path
    if(move_uploaded_file($source, $target_path)) {
        if ($name[1] == 'zip'){
            $zip = new ZipArchive();
            $x = $zip->open($target_path);
            if ($x === true) {
                $zip->extractTo("C:/xampp/htdocs/zip"); // change this to the correct site path
                if ($zip->open($target_path) == TRUE) {
                    for ($i = 0; $i < $zip->numFiles; $i++) {
                        $filename2 = $zip->getNameIndex($i);
                        $tmp_dir = $target_path.$filename2;
                    }
                }
                $zip->close();
                $message = "Your .zip file was uploaded and unpacked.";
                unlink($target_path);
            }
        }
        $message = "Success!";
        } else {    
            $message = "There was a problem with the upload. Please try again.";
        }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

after running the code in php 5, this error shows up: Warning: require_once(System.php): failed to open stream: No such file or directory in C:\wamp64\www\zip\index.php on line 2 AND Fatal error: require_once(): Failed opening required 'System.php' (include_path='.;C:\php\pear') in C:\wamp64\www\zip\index.php on line 2

anyone have a better Idea?

kairi
  • 298
  • 3
  • 16
  • 1
    and where is your "System.php" located? – ino Dec 03 '18 at 07:16
  • if its a path issue, run get_include_path() on both environment ( php 7 and php 5.6) and check whether both results are same or not. – Pritam Jinal Dec 03 '18 at 07:19
  • this may sound dumb, but I never really added system.php in my PHP7 but it worked. :( So i tried in my PHP5, it doesn't. I saw it here https://stackoverflow.com/questions/12874883/creating-temp-directory-on-server – kairi Dec 03 '18 at 07:20
  • ohhh... its not a part of core php. the link from where you copied is of pear php(https://en.wikipedia.org/wiki/PEAR) – Pritam Jinal Dec 03 '18 at 07:29

0 Answers0