我正在尝试取消归档另一个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
答案 0 :(得分:0)
我打算投票删除这个问题,但我认为从长远来看最好留下来。
答案是
How to get PHP ZipArchive to work with variable
似乎整个名称不能作为变量。但如果您将该名称的一部分包含在内,则允许使用。
$zipObj = getcwd()."/update/prescription/".$fileName;
它必须像
一样 $z->open("update/".$r.".zip"){
//do something here
}