我正试图解压缩另一个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:抛出错误。我以为可能是路径问题。所以我换了第二根绳子,希望能解决问题,但没有。所以,举起我的双手,请求另一双眼睛来关注这个问题。
UPDATE ERROR LOG ENTRY
[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
}