No error when creating zip, but it doesn't get created

前端 未结 6 1190
醉梦人生
醉梦人生 2021-02-18 15:38

I wrote this code to create a ZIP file and to save it. But somehow it just doesn\'t show any error, but it doesn\'t create a ZIP file either. Here\'s the code:

$         


        
相关标签:
6条回答
  • 2021-02-18 16:05

    I had an exactly same issue, even when with full writing/reading permissions.

    Solved by creating the ".zip" file manually before passing it to ZipArchive:

    $zip = new ZipArchive;
    $time = microtime(true);
    $path = "maps/zips/test_" . $time . ".zip"
    
    touch($path);  //<--- this line creates the file
    
    $res = $zip->open($path, ZipArchive::CREATE);
    if ($res === TRUE) {
        echo "RESULT TRUE...";
        $zip->addFile("maps/filename.ogz","filename.ogz"); //Sauerbraten map format
        $zip->addFromString('how_to_install.txt', 'Some Explanation...');
        $zip->close();
        $zip_created = true;
        echo "FILE ADDED!";
    }
    
    0 讨论(0)
  • 2021-02-18 16:15

    There are 2 cases when zip doesn't generate the error.

    1. Make sure every file you are adding to the zip is valid. Even if one file is not available when
      zip->close is called then the archive will fail and your zip file won't be created.
    2. If your folder doesn't have write permissions zip will not report the error. It will finish but nothing will be created.
    0 讨论(0)
  • 2021-02-18 16:16

    Probably apache or php has not got permissions to create zip archives in that directory. From one of the comments on ZipArchice::open:

    If the directory you are writing or saving into does not have the correct permissions set, you won't get any error messages and it will look like everything worked fine... except it won't have changed!

    Instead make sure you collect the return value of ZipArchive::close(). If it is false... it didn't work.

    Add an else clause to your if statement and dump $res to see the results:

    if($res === TRUE) {
        ...
    } else {
        var_dump($res);
    }
    
    0 讨论(0)
  • 2021-02-18 16:21

    Check out that each of your file exists before calling $zip->addFile otherwise the zip won't be generated and no error message will be displayed.

    if(file_exists($fichier->url))
    {
        if($zip->addFile($fichier->url,$fichier->nom))
        {
            $erreur_ouverture = false;
        }
        else
        {
            $erreur_ouverture = true;
            echo 'Open error : '.$fichier->url;
        }
    }
    else
    {
        echo 'File '.$fichier->url.' not found';
    }
    
    0 讨论(0)
  • 2021-02-18 16:24

    break it into steps.

    if ($res === TRUE) {
    
      check if file_exist
    
    check if addFile give any error
    }
    
    if($zip->close())
    {
     $zip_created = true; 
        echo "FILE ADDED!"
    }
    

    Check the phpinfo for zip is enabled or not :)

    0 讨论(0)
  • 2021-02-18 16:24

    One of the reasons for zip file is not created is due to missing check if you are adding file and not a directory.

    if (!$file->isDir())
    

    I found the solution here.

    0 讨论(0)
提交回复
热议问题