2013-07-23 48 views
1

我遇到这个错误:PHP ziparchive ReadFile的,没有这样的文件中发现错误

[23-Jul-2013 16:26:15 America/New_York] PHP Warning: readfile(Resumes.zip): failed to open stream: No such file or directory in /home/mcaplace/public_html/download.php on line 24

这是代码:

<?php 
    /*foreach($_POST['files'] as $check) { 
     echo $check;}*/ 
    function zipFilesAndDownload($file_names,$archive_file_name,$file_path) 
    { 
     $zip = new ZipArchive(); 
     //create the file and throw the error if unsuccessful 
     if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE)!==TRUE) { 
     exit("cannot open <$archive_file_name>\n"); 
     } 
     //add each files of $file_name array to archive 
     foreach($file_names as $files) 
     { 
     $zip->addFile($file_path.$files,$files); 
     /*if(file_exists($file_path.$files)) //." ".$files."<br>"; 
      echo "yes";*/ 
     } 
     $zip->close(); 
    //then send the headers to foce download the zip file 
     header("Content-type: application/zip"); 
     header("Content-Disposition: attachment; filename=$archive_file_name"); 
     header("Pragma: no-cache"); 
     header("Expires: 0"); 
     readfile($archive_file_name); 
     exit; 
    } 
    //If you are passing the file names to thae array directly use the following method 
    $file_names = $_POST['files']; 
    //if you are getting the file name from database means use the following method 
    //Include DB connection 

    $archive_file_name='Resumes.zip'; 
    $file_path=$_SERVER['DOCUMENT_ROOT']."/resumes/"; 
    zipFilesAndDownload($file_names,$archive_file_name,$file_path); 
?> 
+0

一个经常运行到这个错误,并迅速解决它,请按照下列步骤操作:http://stackoverflow.com/a/36577021/2873507 –

回答

0

你有几件事情错 首先ZIPARCHIVE :: CREATE是不正确的,它应该是ZipArchive :: CREATE或ZipArchive :: OVERWRITE(覆盖,如果你没有删除zip文件)。

接下来你的路径是错误的。你不告诉readfile()文件的绝对路径。因此,您需要提供绝对路径来指定zip文件的保存位置和读取位置。

此外,我觉得你的网站的根文件夹是可写的,这是不明智的。所以我把zip放到简历文件夹下面的代码中。我倾向于远离$ _SERVER ['DOCUMENT_ROOT']我的自我,并使用realpath()函数。

最后,你必须确定什么文件夹的zip文件正在被创建成它拥有777权限。

下面是完整的代码更改

<?php 
    /*foreach($_POST['files'] as $check) { 
     echo $check;}*/ 
    function zipFilesAndDownload($file_names,$archive_file_name,$file_path) 
    { 
     $zip = new ZipArchive(); 
     //create the file and throw the error if unsuccessful 
     if ($zip->open($file_path.$archive_file_name, ZipArchive::OVERWRITE)!==TRUE) { 
      exit("cannot open <$archive_file_name>\n"); 
     } 
     //add each files of $file_name array to archive 
     foreach($file_names as $files) 
     { 
      $zip->addFile($file_path.$files,$files); 
      /*if(file_exists($file_path.$files)) //." ".$files."<br>"; 
       echo "yes";*/ 
     } 
     $zip->close(); 
    //then send the headers to foce download the zip file 
     header("Content-type: application/zip"); 
     header("Content-Disposition: attachment; filename=$archive_file_name"); 
     header("Pragma: no-cache"); 
     header("Expires: 0"); 
     readfile($file_path.$archive_file_name); 
     exit; 
    } 
    //If you are passing the file names to thae array directly use the following method 
    $file_names = array('test.txt'); 
    //if you are getting the file name from database means use the following method 
    //Include DB connection 

    $archive_file_name='Resumes.zip'; 
    $file_path=$_SERVER['DOCUMENT_ROOT']."/resumes/"; 

    zipFilesAndDownload($file_names,$archive_file_name,$file_path); 
?> 
+0

同样的错误遗体即使在更改文件夹的权限之后。 – user2612159

+0

奇怪的是,我运行代码以确保它在发布之前运行。你能打印出路径并确保它们与文件夹结构正确匹配吗?还要确保正在创建zip文件。 – Pyromanci

+0

现在,此处有一个有关此频繁错误的故障排除清单:stackoverflow.com/a/36577021/2873507 –

相关问题