[英]PHP Upload and Extract Zip
我正在尝试运行一个允许上传 a.zip 并提取内容的脚本。 我在网上获取了一个应该可以工作的示例代码,并在开头添加了一个 class,因为我的 ISP 没有正确编译 zip 功能。
我在中间留下了一个很大的评论,我被卡住了。 不确定这是否与在 IIS 上运行有关?
.zip 文件上传到我期望的位置,我可以手动将其 ftp 恢复并提取文件。 我希望在这里提取文件,循环遍历它们,如果它们是图像文件,然后将它们添加到画廊图像数据库中......首先要做的事情。 需要了解为什么我没有看到 zip 的内容...
<?php // need this bc of ISP settings
require($_SERVER['DOCUMENT_ROOT']."/_classes/ZipArchive.php");
?><?php
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
// I set up the _TEST dir with 777 permissions
$target_path = $_SERVER['DOCUMENT_ROOT']."/_TEST/".$filename;
if(move_uploaded_file($source, $target_path)) {
$zip = new ZipArchive();
$x = $zip->open($target_path);
// **********************************************************
// $x returns an error here
// code: ER_OPEN
// http://php.net/manual/en/function.ziparchive-open.php
// Not sure why?????
// **********************************************************
if ($x === true) {
$zip->extractTo($_SERVER['DOCUMENT_ROOT']."/_TEST/");
$zip->close();
unlink($target_path);
$message = "Your .zip file was uploaded and unpacked.";
}
else {
$message = 'failed';
}
} else {
$message = "There was a problem with the upload. Please try again.";
}
}
?>
不是解决方案,而是解决方法:您可以控制机器吗? 如果是这样,请安装 7-zip(在 Windows 上)或unzip
,使用system('unzip...')
或system('7z...')
来提取 zip 存档。
ISP 安装了 zip 工作所需的组件,所以现在一切正常。 感谢@timdream 提供另一种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.