Reputation: 1034
I have to download multiple csv file at once. So, I create zip file and put all of the csv files into that zip file. Everything is work and I can download my zip file and can open on my localhost. And I used windows OS.
Here is my code from export_csv.php
.
for($i=0; $i<$len; $i++) {
$user_id = $array[$i];
#get user name
$name = "...";
#get all day from selected month with holidays
$day_of_month_arr = allDay_of_month($year,$month);
#get user datetime
#prepare start and finish time with holidays and weekend
$result_arr = time_format($result, $day_of_month_arr);
#prepare data as csv format to export as csv
$exp = export($result_arr);
#put each user csv file into 'Report.zip'
#archive all csv file as zip and force download that zip file
$zipname = 'Report.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
$f = fopen('php://memory', 'w');
$file_name = $name."-".$user_id.".csv";
foreach ($exp as $arr) {
fputcsv($f,$arr);
}
rewind($f);
$zip->addFromString($file_name, stream_get_contents($f));
//close the file
fclose($f);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
// remove the zip archive
unlink($zipname);
function export() {.......}
function time_format() {........}
function allDay_of_month() {......}
So, I upload that script export_csv.php
to my ubuntu server(production server). And when I try to download this file from production server, I can download the file but can't open this zip file anymore. It shows "......\Report.zip" is invalid"
.
When I check whats wrong in php_error_logs
, I found this following problems.
[12-Oct-2016 04:09:43 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:43 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:43 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: main(): Cannot destroy the zip context in /opt/lampp/htdocs/project/export_csv.php on line 48
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /opt/lampp/htdocs/project/export_csv.php on line 62
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: filesize(): stat failed for Report.zip in /opt/lampp/htdocs/project/export_csv.php on line 65
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: readfile(Report.zip): failed to open stream: No such file or directory in /opt/lampp/htdocs/project/export_csv.php on line 66
[12-Oct-2016 04:09:44 Europe/Berlin] PHP Warning: unlink(Report.zip): No such file or directory in /opt/lampp/htdocs/project/export_csv.php on line 69
I think this error is permission problem. But I am newbie in ubuntu OS and so, I don't know how to solve it.
I'm very appreciate for any help.
Update
I add the exact error when I try to download and open zip file from server.
Upvotes: 2
Views: 1693
Reputation: 6372
Seems like your php server is not permitted to write to the path you are giving it. You need to add write permission to the user that runs the php server to this path: /opt/lampp/htdocs/project/
You should use the chmod linux command. The linux user that runs the php shall have write permission to that path.
http://ss64.com/bash/chmod.html
https://www.linux.com/learn/understanding-linux-file-permissions
Upvotes: 2