我必須一次下載多個csv文件。所以,我創建了zip文件並將所有csv文件放入該zip文件中。一切工作,我可以下載我的zip文件,並可以在我的本地主機上打開。我使用Windows操作系統。無法在ubuntu服務器上打開zip文件錯誤
這是我的代碼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() {......}
所以,我上傳腳本export_csv.php
到我的Ubuntu的服務器(生產服務器)。當我嘗試從生產服務器下載此文件時,我可以下載該文件,但無法再打開此zip文件。它顯示"......\Report.zip" is invalid"
。
當我檢查php_error_logs
有什麼問題時,我發現了下列問題。
[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
我認爲這個錯誤是權限問題。但我是Ubuntu操作系統的新手,所以我不知道如何解決它。
我很感激任何幫助。
更新
我添加了確切的錯誤,當我嘗試下載並從服務器上打開zip文件。
試着給你的文件777個權限。 – IsThisJavascript
應該小心...... – gba
@ WillParky93我已經添加了777個權限'sudo chmod 777/opt/lampp/htdocs/project/export_csv.php'。但它仍然是一樣的錯誤。 – Cloud