【问题标题】:PHP extracting files from zip issues with copyPHP从带有复制的zip问题中提取文件
【发布时间】:2014-07-24 02:48:25
【问题描述】:

我正在尝试通过 PHP 从 zip 中提取文件,我有一些工作代码,但是当我将它从一台服务器移动到另一台服务器时,它突然停止工作:( 在这段代码中,它创建了文件夹,我可以看到它这样做了,但未提取 zip 中的文件:(

这是我的代码:

if($_FILES['url']['error'] == 0){
    system('rm -rf ../images/galleries/' . $galleryName);
    $filename = $_FILES["url"]["name"];
    $source = $_FILES["url"]["tmp_name"];
    $type = $_FILES["url"]["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 (!file_exists('../images/galleries/' . $galleryName)) {
        mkdir('../images/galleries/' . $galleryName, 0777, true);
    }
    $target_path = '../images/galleries/' . $galleryName . '/' . $filename;
    $image = $filename;

    if(move_uploaded_file($source, $target_path)) {
        $path = $target_path;
        $zip = new ZipArchive();
        if ($zip->open($path) === true) {
            for($i = 0; $i < $zip->numFiles; $i++) {
                $filename = $zip->getNameIndex($i);
                $fileinfo = pathinfo($filename);
                $imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_")) . '_';
                copy("zip://".$path."#".$filename, '../images/galleries/' . $galleryName . '/' . $fileinfo['basename']);
                chmod('../images/galleries/' . $galleryName . '/' . $fileinfo['basename'], 0777);
            }
            $zip->close();
        }
    }
    unlink($path);
    $galleryClass->insertImage($connection, $_POST['galleryId'], $image, $_POST['link'], $_POST['order']);
}

我认为问题在于:

copy("zip://".$path."#".$filename, '../images/galleries/' . $galleryName . '/' . $fileinfo['basename']);

还有其他方法可以做到这一点还是这是服务器问题?

【问题讨论】:

  • 你使用copy()而不是$zip-&gt;extractTo()有什么原因吗?
  • 不,没有理由,我将如何在这段代码中使用它?
  • 找到了这个例子 php.net/manual/en/ziparchive.extractto.php 并且成功了 :)
  • 在答案 cOle2 中输入一个答案,我会检查它:)

标签: php zip


【解决方案1】:

尝试使用extractTo() 方法而不是copy()

$zip->extractTo('../images/galleries/' . $galleryName . '/' . $fileinfo['basename'], $filename);

【讨论】:

  • 如果我把它放在循环中,它会将每个文件作为文件夹插入,并且文件在每个文件夹中:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多