繁体   English   中英

第二个Zip文件抛出无效或单元化的Zip对象

[英]Second Zip file throwing Invalid or unitialized Zip Object

我试图取消存档另一个zip文件中的zip文件,以获取第二个zip文件中的xml文件。

该文件的最大挑战是zip文件中的文件将始终是不同的,因此是未知的。 因此,我创建了一个将存档档案列表放入文本列表的功能。 然后,使用此列表取消存档每个文件,并从第二个zip文件中的xml文件中提取所需的信息。

到目前为止,这是我的代码。

//Set the date
$day = date("mdY");
echo $day."<br>";

//URL to download file from for updating the prescription table
//$url = "ftp://public.nlm.nih.gov/nlmdata/.dailymed/dm_spl_daily_update_".$day.".zip";
  $url = "ftp://public.nlm.nih.gov/nlmdata/.dailymed/dm_spl_daily_update_09152016.zip";

//Saving the file on the server. 
 file_put_contents("Prescription_update.zip", fopen($url, 'r'));

//unzip the downloaded file
 $zip = new ZipArchive;

 if($zip->open('Prescription_update.zip')){
      $path = getcwd() . "/update/";
      $path = str_replace("\\","/",$path); 
     //echo $path;
      $zip->extractTo($path);
      $zip->close();
     print 'ok<br>';
  } else {
     print 'Failed';
}


// integer starts at 0 before counting
$i = 0; 
$dir = '/update/prescription/';
if ($handle = opendir($dir)) {
    while (($file = readdir($handle)) !== false){
        if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
            $i++;
    }
}
// prints out how many were in the directory need this for the loop later
   echo "There were $i files<br>";


 $dh  = opendir($dir);
 $files = array();
 while (false !== ($filename = readdir($dh))) {
     $files[] = $filename." \r\n";
 }
  //Created a list of files in the update/prescription folder
   file_put_contents("list.txt", $files);


 /*
  *  Creating a loop here to ready the names and extract the 
  *  XML file from the zipped files that are in the update/prescription folder
  *
  */

  $ii = 2;
  $fileName = new SplFileObject('list.txt');
  $fileName->seek($ii);
  echo $fileName."<br>";   //return the first file name from list.txt

  $zip = new ZipArchive;

  $zipObj = getcwd()."/update/prescription/".$fileName;
  $zipObj = str_replace("\\","/", $zipObj);
  echo $zipObj."<br>";
 if($zip->open($zipObj)){
       $path = getcwd() . "/update/prescription/tmp/";
       $path = str_replace("\\","/",$path); 
       mkdir($path);
       echo $path;
       $zip->extractTo($path);
       $zip->close();
       print 'ok<br>';
    } else {
       print 'Failed';
  }

无法弄清楚为什么第二个ZipArchive :: extractTo:引发错误。 我认为这可能是路径问题。 因此,我做了第二次字符串替换,希望可以将其清除,但事实并非如此。 因此,举起我的手,并要求另一只眼睛盯着它。

更新错误日志条目

   [18-Sep-2016 02:24:24 America/Chicago] PHP   1. {main}() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:0

   [18-Sep-2016 02:24:24 America/Chicago] PHP   2. ZipArchive->extractTo() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:76

   [18-Sep-2016 02:24:24 America/Chicago] PHP Warning:  ZipArchive::close(): Invalid or unitialized Zip object in C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php on line 77

 [18-Sep-2016 02:24:24 America/Chicago] PHP Stack trace:

 [18-Sep-2016 02:24:24 America/Chicago] PHP   1. {main}() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:0

 [18-Sep-2016 02:24:24 America/Chicago] PHP   2. ZipArchive->close() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:77

我本打算投票删除该问题,但从长远来看,最好还是保留它。

答案是

如何使PHP ZipArchive与变量一起使用

似乎不能将整个名称作为一个变量来表示。 但是,如果您使用名称的一部分,则可以使用。

 $zipObj = getcwd()."/update/prescription/".$fileName;

它必须像

 $z->open("update/".$r.".zip"){
    //do something here
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM