【问题标题】:Second Zip file throwing Invalid or unitialized Zip Object第二个 Zip 文件抛出无效或未初始化的 Zip 对象
【发布时间】:2016-09-18 06:43:30
【问题描述】:

我正在尝试解压缩另一个 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 zip


【解决方案1】:

我打算投票删除这个问题,但我认为从长远来看最好保留它。

答案是

How to get PHP ZipArchive to work with variable

似乎不能将整个名称替换为变量。但是,如果您使用子名称的一部分,它是允许的。

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

它必须被替换

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2023-03-24
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多