2016-10-11 31 views
1

複数のcsvファイルを一度にダウンロードする必要があります。だから、私はzipファイルを作成し、すべてのcsvファイルをそのzipファイルに入れます。すべてが動作し、私はzipファイルをダウンロードでき、自分のlocalhostで開くことができます。私はWindows OSを使いました。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() {......} 

だから、私はUbuntuのサーバ(運用サーバー)にそのスクリプトexport_csv.phpをアップロードします。そして、プロダクションサーバーからこのファイルをダウンロードしようとすると、ファイルをダウンロードできますが、この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 OSの初心者なので、解決方法はわかりません。

非常に助けていただきありがとうございます。

更新

私は、サーバーからzipファイルをダウンロードして開こうとすると、私は正確なエラーを追加します。

+1

あなたのファイルに777回のアクセス権を与えてみてください。 – IsThisJavascript

+1

はそれに注意する必要があります... – gba

+0

@ WillParky93私は777の権限 'sudo chmod 777/opt/lampp/htdocs/project/export_csv.php'をすでに追加しています。しかし、それはまだ同じエラーです。 – Cloud

答えて

1

あなたのPHPサーバのように、あなたがそれを与えるパスに書き込むことはできません。 このパスにPHPサーバを実行するユーザに書き込み権限を追加する必要があります。/ opt/lampp/htdocs/project/

chmod linuxコマンドを使用してください。 PHPを実行するLinuxユーザーは、そのパスへの書き込み許可を持っていなければなりません。

http://ss64.com/bash/chmod.html

https://www.linux.com/learn/understanding-linux-file-permissions

+0

ありがとう@gba。プロジェクトフォルダに777パーミッションを追加した後は、すべて問題ありません:) – Cloud

関連する問題