What can be the matter: when creating an archive (from one file) an empty zip is created. If you feed him the path to a file that does not exist, the archive is not created at all, which is logical. If the path is specified from the root of the home directory /home/www/citename/files/date/file.ext , an empty zip appears. If you specify the path relative to the root of the site /files/date/file.ext , the matryoshka from the directories appears in the archive, at the end of which there is the necessary file. What's my mistake?

 <?php $zipArchiveName = $fileName .".zip"; $zip = new ZipArchive(); $archive = $tempFolder . "/" . $zipArchiveName; if ($zip->open($archive, ZipArchive::CREATE)!==TRUE) { return; } if( !$zip->addFile( $filePath ) ){ return; } $zip->close(); 

    1 answer 1

    This method should work 100%. If you still do not register, check: 1. Permissions to write to the destination directory and read the source.
    2. Whether the PHP extension is connected (this is not a given case, since your archive is still being created, but someone may have a problem with that). 3. Are the names of files and directories correct?

     <?php $tempFolder = '/home/user/'; $archiveName = 'archive.zip'; $filePath = '/home/user/1.txt'; $zip = new ZipArchive(); $archive = $tempFolder . $archiveName; if (!file_exists($archive)) { file_put_contents($archive, null); } if ($zip->open($archive, ZipArchive::CREATE) !== true) { exit ('Error open'); } if (!$zip->addFile($filePath, basename($filePath))) { exit ('Error adding'); } $zip->close(); 
    • Thank. Your code worked. It is a pity, but it is not clear what is wrong with mine. Compare all the paths by character. - Andriy
    • The problem was not with the rights, then? - Daniel-664
    • Absolutely. My continues to not work. On the right, I sinned before turning here. - Andriy
    • Well, besides this, I just added the creation of an archive if (!file_exists($archive)) { file_put_contents($archive, null); } if (!file_exists($archive)) { file_put_contents($archive, null); } and if (!$zip->addFile($filePath, basename($filePath))) { specify the file name in the archive - Daniel-664
    • First of all, after a successful test, I removed this block. But nat) without it worked the same way. - Andriy