Search code examples
phpphp-ziparchive

zipArchive in PHP is not working in server but on localhost runs and extracts the file


I have this application that fetches the data from outlook mail from sendthisfile.com.

Now in my localhost machine when I test the application, the zipArchive works well and the file extracts to my respective localhost folder. Now when I tried to run it on my server, the zipArchive is not working well. There is no error on it. There is a filename on $zip->open($filename).

My question is why can't it extract on my server?

My code below:

$path = "my-folder/"; $zip = new ZipArchive; $resFile = $zip->open($filename);

if ($resFile === TRUE) {
    $zip->extractTo($path);
    $zip->close();
    echo "Woot successfully extract $filename to $path";
    echo "<br />";
} else {
    echo "Error opening the file $filename";
}

Can someone help me figured this out?


Solution

  • Suppose phpArchive has been enabled, and you have set a write permission to directory, it's likely to be path problem.

    Try to use $_SERVER['DOCUMENT_ROOT'] and try to run this code.

    $path = "my-folder/";
    $zip = new ZipArchive;
    $resFile = $zip->open($filename);
    
    if ($resFile === true) {
        if ($zip->extractTo($_SERVER['DOCUMENT_ROOT'].'/'.$path) === false) {
            echo "Extract Failed!<br/>";
        }
        else{
            echo "Woot successfully extract $filename to $path<br/>";
        }
        $zip->close();
    }
    else{
        echo "Error opening the file $filename";
    }