Search code examples
phpphp-ziparchive

Why ZipArchive working not correct?


This is my code zip 1 file:

 $zipname="C:/xampp/htdocs/test/5/JPN/5/5_1.0.pdf.zip"
    $zip = new ZipArchive;
         $zip->open($zipname, ZipArchive::CREATE);                     
         $zip->addFile("C:/xampp/htdocs/test/5/JPN/5/5_1.0.pdf");       
         $zip->close();

But it zip from folder C:\ enter image description here

Why ZipArchive working not correct?


Solution

  • You need to pass two parameters in the addFile function.

    According to the documentation

    bool ZipArchive::addFile ( string $filename [, string $localname ] )

    filename The path to the file to add.

    localname local name inside ZIP archive.

    This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

    The following code will work for you

    $zip->addFile("C:/xampp/htdocs/test/5/JPN/5/5_1.0.pdf", "5_1.0.pdf");