skip to Main Content

I am trying to upload a .zip file to a Server and extract it.

First, I move the uploaded .zip file to my $target_path

$target_Path = $path . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $target_Path);

Then I am trying to unzip the .zip file using ZipArchive in PHP (ZipArchive)

 function unzip($zipFile, $destination) {
                $zip = new ZipArchive();
                $zip->open($zipFile);
                for($i=0; $i<$zip->numFiles; $i++) {
                    $file=$zip->getNameIndex($i);
                    if(substr($file,-1) == '/') continue;
                    $lastDelimiterPos = strrpos($destination.$file, '/');
                    $dir = substr($destination.$file, 0, $lastDelimiterPos);
                    if (!file_exists($dir)) {
                        mkdir($dir, 0777, true);
                    }
                    $name = substr($destination.$file, $lastDelimiterPos + 1);
                    echo $dir. "/" .$name;
                    copy("zip://$zipFile#$file","$dir/$name");
                }

                $zip->close();
            }

            unzip($target_Path, $path);

$target_path is a relative path directly to the .zip file

$path is a relative path to the folder ending in a "/"

Through the Error-Message I know, that my path to the .zip file must be correct (I see the files I want to copy)
The Error I get:

<br>
copy(zip://../Customer/Test/Screen Links/HTML_Content/WETTERKARTE.zip#WETTERKARTE/alt/fs_panel.svg): Failed to open stream: operation failed in D:xampphtdocsMVM_RED_VIOLETphpAX_upload_PIC.php on line 60 <br>

So I know that fs_panel.svg (the first file in the ZipArchive) gets found.

I just don´t know how I can copy the file from the ZipArchive to a folder outside.

I also tried zip->extractTo – that only gives me no error but also no files – it just says it worked without doing anything.

2

Answers


  1. Chosen as BEST ANSWER

    I could not get the zipArchive to work so I looked for other options...

    I solved my problem using shell_exec()

    shell_exec("7z x $file -o$path -y 2>&1");
    

    this command uses 7-Zip (which is installed on my Pc) and extracts a $file to my $path

    Things you need to set for this to work:

    • install and add 7-zip to your enviroment variables
    • change variables_order="GPCS" to variables_order="EGPCS" in your php.ini file
    • set safe_mode_exec_dir=Off in your php.ini file
    • add 2>&1 to the end of you command

    this setup executes any command you write in your php file in a shell - so if a command works in your shell it should work in php too


  2. Try this:

    <?php
    function unzip($zipFile, $destination) {
        $zip = new ZipArchive();
        
        // Try to open the zip file
        if ($zip->open($zipFile) === TRUE) {
            // Check if the destination directory exists, if not create it
            if (!file_exists($destination)) {
                mkdir($destination, 0777, true);
            }
    
            // Extract the contents of the zip file to the destination
            if ($zip->extractTo($destination)) {
                echo "Files extracted successfully to $destination.";
            } else {
                echo "Failed to extract files.";
            }
    
            // Close the zip archive
            $zip->close();
        } else {
            echo "Failed to open the zip file.";
        }
    }
    
    // Example usage
    $target_path = $path . basename($_FILES['file']['name']);  // Path where the uploaded zip is saved
    move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
    
    unzip($target_path, $path);  // Extract the zip file to the destination folder
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search