簡體   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