Unlink deletes more than just the files transferred to it

I am creating a SQL file by putting this file in a zip file with some images and then deleting the SQL file using unink.Strange it deletes the zip file as well.

if (file_put_contents($sqlFileName, $sql) !== false) {
        $zip = new ZipArchive;
        if ($zip->open($workingDir . $now . '.zip', ZipArchive::CREATE) === true) {
            $zip->addFile($sqlFileName, basename($sqlFileName));
            if(! empty($images)) {
                foreach ($images as $image) {
                    $zip->addFile($imagesDir . $image, $image);
                }
            }
        }
        unlink($sqlFileName);
}

      

+2


source to share


2 answers


As written at http://www.php.net/manual/en/function.ziparchive-addfromstring.php :

jared at kippage dot com 03-Sep-2009 01:08

It may seem a little obvious to some, but it was control over my name.

If you are adding files to the zip file that you want to delete, make sure you delete AFTER you call close ().

If the files added to the object are not available during the save, the zip file will not be created.



Simple solution: add $zip->close()

before you call unlink($sqlFileName);

and you should be fine.

+2


source


unlink()

never deletes more than one file. Your zip file is not being created in the first place because you are not closing the archive, which basically tells PHP to wrap the archive headers, do compression, etc. Add $zip->close()

to your code.



UPDATE I just read that ZipArchive :: close () is called automatically at the end of the script, so this leaves only two options: either your call zip->open()

fails. or your code doesn't go into the foreach loop, leaving you with an empty archive. Empty archives are not created according to the comment in the doc function .

+1


source







All Articles