Hallo,
Ik probeer om met PHP alle bestanden in een bepaalde directory in een zip-bestand te plaatsen. Hiervoor heb ik deze functie
PHP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function create_zip($dir) { if (is_writable($dir)) { $zip = new ZipArchive; $res = $zip->open($dir . '/test.zip', ZipArchive::CREATE); echo $zip->getStatusString(); if ($res === true) { if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { if (!$zip->addFile($entry)) { die ("Couldn't add file to zip archive"); } echo $zip->getStatusString(); } } closedir($handle); } else { die ("Couldn't open dir"); } if (!$zip->close()) { die ("Couldn't save zip file"); } echo $zip->getStatusString(); echo "zip created"; } else { die ("Couldn't create zip file"); } } }
Als ik deze functie aanroep vanuit mijn script is dit de uitvoer
PHP kan dus schrijven naar de directory, overal is de status "No error", maar bij $zip->close gaat er iets mis en wordt er false opgeleverd. Het zip-bestand is ook niet te vinden in de directory.No errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorNo errorCouldn't save zip file
Hoewel ik helemaal bovenin het php-bestand
heb staan, krijg ik geen foutmelding te zien. Ook in de access_log en error_log van Apache is niet te zien waarom het misgaat.PHP Code:
1 2 ini_set('display_errors', 'On'); error_reporting(E_ALL);
Wat zou het probleem kunnen zijn of hoe kan ik zien wat er misgaat?
Bij voorbaat dank.
Born to be root.
Wat gebeurt er als je
verandert inCode:$res = $zip->open($dir . '/test.zip', ZipArchive::CREATE);
Code:$res = $zip->open($dir . 'test.zip', ZipArchive::CREATE);
Heb je wel rechten om in de desbetreffende dir te schrijven? ik zie alleen maar dat je leesrechten hebt gecontroleerd
Dankjewel voor jullie antwoorden, maar ik heb het probleem net opgelost via deze comment op php.net
De bestanden bestonden wel, maar readdir levert de bestandsnamen op zonder pad. Daarom kon addFile de bestanden niet vinden en faalde de close. Ik heb het opgelost door de regelIf you're adding multiple files to a zip and your $zip->close() call is returning FALSE, ensure that all the files you added actually exist. Apparently $zip->addFile() returns TRUE even if the file doesn't actually exist. It's a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.te veranderen inPHP Code:
1 if (!$zip->addFile($entry))PHP Code:
1 if (!$zip->addFile($dir . '/' . $entry, $entry))
Born to be root.