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 files at once.
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 "error opening file: some/path/: Permission Denied". 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 >= 0; $x--) {
$pdf = new Imagick($imageFilesArray[$x]);
$pdf->setImageFormat('pdf');
$pdf->writeImages("../upload/converted-{$x}.pdf", true);
//chmod("../upload/converted-{$x}.pdf", 0777);
array_push($pdfFilesArray,"../upload/converted-{$x}.pdf");
}
$zip = new ZipArchive();
$tmp_file = tempnam('.', '');
chmod($tmp_file, 0777);
$zip->open($tmp_file, ZipArchive::CREATE);
foreach ($pdfFilesArray as $pdf) {
$download_file = file_get_contents($pdf);
$zip->addFromString(basename($pdf), $download_file);
}
$zip->close();
header('Content-disposition: attachment; filename="Splitted-PDFs.zip"');
header('Content-type: application/zip');
readfile($tmp_file);
unlink($tmp_file);
How do I unzip a 5GB zip file on my web server without PHP timeout?
I need to unzip a 5GB zip file on server.
When I run the unzip it times out because the file extraction takes too long.
I do not have access to shell console, all I have is a FTP and I can execute PHP by accessing the PHP file in my web browser.
How do I unzip the large file?
$dirName = $_POST["dirName"];
echo "$dirName<br>\n";
$dirName = htmlspecialchars($dirName);
echo "$dirName<br>\n";
flush();
$zipFileName = getcwd() . "/$dirName/aTest.zip";
$destDirName = getcwd() . "/$dirName/aTest";
echo "Zip file is $zipFileName<br>\n";
echo "Directory is $destDirName<br>\n";
flush();
// Unzip selected zip file
$zip = new ZipArchive;
$res = $zip->open($zipFileName);
if ($res === TRUE) {
// Extract file
$zip->extractTo($destDirName);
$zip->close();
}
echo "Successfull<br>\n";
flush();
I found an answer here but the link ("try PclZip") does not work.
following is the restrictions as per PHP Info
disable_functions
exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,
proc_open,ini_alter,dl,popen,popen,pcntl_exec,socket_accept,socket_bind,
socket_clear_error,socket_close,socket_connect,socket_create_listen,
socket_create_pair,socket_create,socket_get_option,socket_getpeername,
socket_getsockname,socket_last_error,socket_listen,socket_read,
socket_recv,socket_recvfrom,socket_select,socket_send,socket_sendto,
socket_set_block,socket_set_nonblock,socket_set_option,socket_shutdown,
socket_strerror,socket_write,stream_socket_server,pfsockopen,
disk_total_space,disk_free_space,chown,diskfreespace,getrusage,
get_current_user,set_time_limit,dl,leak,listen,chgrp,link,symlink,
dlopen,proc_nice,proc_get_stats,proc_terminate,shell_exec,sh2_exec,
posix_getpwuid,posix_getgrgid,posix_kill,ini_restore,mkfifo,dbmopen,
dbase_open,filepro,filepro_rowcount,posix_mkfifo,_xyec,mainwork,
get_num_redirects,putenv,geoip_open,sleep,imap_open
You can try to change max-execution-time value with
<?php
ini_set('max-execution-time', 600); // 600 seconds
If it's not working, you should ask to the administrator of the server.
I want to zip a file using PHP. The following code works fine with XAMPP on my local machine, but not on hosting site.
First I thouught it was file permission problem, but I changed it to 777 and nothing changed.
Secondly, I read somewhere on Internet that zip extension may not be loaded but funtcion extension_loaded('zip') return true and there is no errors.
Thirdly, I have checked the result of zipArchive funtions - in my case, $zip->open() returns true, $zip->addFile() return false and $zip->close() return true. But zip file isn't created. It seems like permissions issue, but I have set 777 for both .php and .txt file which I want to add to a zip file.
PHP version on my local machine: 7.3.4
PHP version on my hosting provider: 7.3.1
$zip = new ZipArchive();
$DelFilePath="raport_X_.zip";
if(file_exists($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$DelFilePath)) {
unlink ($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$DelFilePath);
}
$openResult = $zip->open($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$DelFilePath, ZIPARCHIVE::CREATE);
$file='BranżowaSzkołaIstopniamechanikpojazdówsamochodowychGIMNAZJUM_RAPORT.txt';
$addResult = $zip->addFile($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$file,$file);
$closeResult = $zip->close();
$path = 'raports/raport_X_.zip';
$filename = "raport_X_.zip";
if(file_exists($path)){
header("Content-Type:application/zip");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-control: private");
header('X-Sendfile: '.$path);
readfile($path);
exit;
}
echo '<br>';
if(extension_loaded('zip')){
echo 'Zip extension is loaded <br>';
}
echo 'Open result:';
var_dump($openResult);
echo 'Add error:';
var_dump($addResult);
echo 'Close error:';
var_dump($closeResult);
phpinfo();
?>
I simply want to get a zip with a .txt file in it. I am getting no error, but zip file isn't created. It works on XAMPP, but not on hosting. I wasted a lot of time on it and now I got no idea.
I have the below code that creates a ZIP file, adds a file to it and then downloads to my computer.
$zip = new ZipArchive();
if ($zip->open('order_sheets.zip', ZipArchive::CREATE) === TRUE){
$zip->addFile($pdfFilePath);
}
$zip->close();
$file_url = 'order_sheets.zip';
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$file_url);
header('Content-Length: ' . filesize($file_url));
readfile($file_url);
All works good but the issue is, when opening the downloaded ZIP, it say's This folder is empty when it is not. If i right click and hit "Extract Here", the contents come out.
Anyone know why that is?
The problem is with Windows' zip utility, which uses IBM850 encoding causing it to misinterpret some characters in internal filenames in the archive, including _ (underscore) as in your file.
See answer here:
PHP ZipArchive Corrupt in Windows
Explained in PHP Manual user notes here: http://php.net/manual/en/ziparchive.addfile.php#95725
I need to unzip file, witch is in remote http server. And I have a code
<?php
$path = "http://mydomain.com/zipfile.zip";
$zip = new ZipArchive;
if ($zip->open($path, ZIPARCHIVE::CREATE) === TRUE)
{
$zip->extractTo('zip/');
$zip->close();
echo "ok';
}
else
{
echo 'failed';
}
?>
The result after execution is "ok", but file did'n extract. Where can be problem?
P.S. in archive is only one .csv file
It seems it unzipped the file in your local machine instead, because you're executing this php locally. You probably will find this file in your PHP temp folder.
If you want it working on server, you should place this file on the server (let's say you call it unzip_file.php), change the $path to absolute path (something like /my/path/zipfile.zip).
You could download it straightway using php headers:
header('Content-Disposition:attachment; filename="' . $sFilename . '"');
Call this program instead: http://mydomain.com/unzip_file.php