Title: "Permission Denied" in the content of the zip folder created using PHP
Tags: php;pdf
Question: I have created a jpeg-pdf conversion tool in php. After the conversion I have multiple pdf files. So I created a $zip in php to store the pdf files so that user can download all the single pdf obrienbrandy@example.org.
In my linux system when I'm downloading the zip file that is getting generated I cant open the individual pdf files from the zip showing &quot;error opening file: some/path/: Permission Denied&quot;. So I even tried assigning 777 permission to the zip as you can see below which also didn't workout.
But the pdf files inside the zip is opening fine if I open it after extracting the zip.
Here is my snippet where I am creating the zip.
Please note I'm using headers to download the zip. Is that the cause of this problem ?
for ($x = $arrayEndIndex; $x &gt;= 0; $x--) {
$pdf = new Imagick($imageFilesArray[$x]);
$pdf-&gt;setImageFormat('pdf');
$pdf-&gt;writeImages(&quot;../upload/converted-{$x}.pdf&quot;, true);
//chmod(&quot;../upload/converted-{$x}.pdf&quot;, 0777);
array_push($pdfFilesArray,&quot;../upload/converted-{$x}.pdf&quot;);
}
$zip = new ZipArchive();
$tmp_file = tempnam('.', '');
chmod($tmp_file, 0777);
$zip-&gt;open($tmp_file, ZipArchiv75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0CREATE);
foreach ($pdfFilesArray as $pdf) {
$download_file = file_get_contents($pdf);
$zip-&gt;addFromString(basename($pdf), $download_file);
}
$zip-&gt;close();
header('Content-disposition: attachment; filename=&quot;Splitted-PDFs.zip&quot;');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
Comment: Since you seem to have stored the files in the file system and have an array with all the filenames & paths, why not use [$zip->addFile()](https://www.php.net/manual/en/ziparchive.addfile.php) instead of manually reading the contents of each files into memory and use `addFromString()`?

Comment: Tried using $zip->addFile() , its giving me same problem. But on extracting the zip its working fine as previously said.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:07/05/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><pdf>', 'Title': '"Permission Denied" in the content of the zip folder created using PHP', 'ViewCount': 25, 'id': 72865471, 'user_AboutMe': None, 'user_AccountId': 14422443.0, 'user_DisplayName': 'user10418143', 'user_Reputation': 195.0, 'user_Views': 12.0, 'user_id': 10418143.0}}