I am using ZipArchive with the following code to create zip files. It works well in all browsers on a Mac. But it says the file is invalid on any browser on a Windows computer. I don't understand why. I emailed the supposedly corrupt file from the Windows computer to myself and opened it on my Mac computer, and it worked fine. I also read through all the suggestions on this thread and tried all of them, with no luck.
Do you see anything wrong with my code?
if(extension_loaded('zip')){
if(isset($post['afiles']) and count($post['afiles']) > 0){
$zip = new ZipArchive();
$url = get_stylesheet_directory();
$filepath = $url;
$zip_name = "DSV".time().".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "Error";
}
foreach($post['afiles'] as $file){
// get the file directory url from the file ID
$path = get_attached_file( $file );
// add each file to the zip file
$zip->addFile($path, basename($path));
}
$zip->close();
ob_clean();
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}else
$error .= "* Please select file to zip <br/><br />";
}else
$error .= "* You do not have the ZIP extension<br/><br />";
I echoed basename($path) to confirm that there are no slashes. It is simply the filename like "this-is-my-file.docx". Thanks for any insight!
Edit: The exact error in Windows says:
Compressed (zipped) Folders Error
Windows cannot open the folder.
The Compressed (zipped) Folder 'C:\Users\krist\Downloads\DWV1620652983.zip' is invalid.
After inspecting the ZIP files, there's HTML coming after the ZIP content. The fix is to make sure to call exit as soon as possible after calling readfile so that nothing else is written to the stream.
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 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 want to download a file using php. Everything is working fine on windows, but when I try to run the same php code on my ubuntu 12.04 just an empty file gets downloaded without any content. When I try to run the following code:
<?php
$filename = '/root/my_folder/filename.pdf';
if(file_exists($filename))
$f = fopen( '/root/my_folder/filename.pdf', 'r') or exit('unable to open file');
else
echo 'file does not exists';
?>
It always display the 'file does not exists'. Is the issue with setting path to my folder ?
Can anyone help me with this. I'm new in Ubuntu. Thanks
your apache user does not hae the permission to access /root/myfolder/filename.pdf . You will have to either add apache user to the group that is accessing /root/myfolder or change the ownership of filename.pdf or change the permissions on /root/myfolder
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