PHP从zip问题中提取文件与副本

时间:2014-07-23 18:26:38

标签: php zip

我试图通过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']);

有另一种方法可以做到这一点,还是这是一个服务器问题?

1 个答案:

答案 0 :(得分:1)

尝试使用extractTo()方法代替copy()

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