user3723240
user3723240

Reputation: 393

PHP extracting files from zip issues with copy

I am trying to extract file from a zip via PHP, I have some working code, but when I move it from one server to another it all of sudden stopped working :( In this code, it creates folders, I can see its doing that, but the files in the zip are not being extracted :(

Here is my code:

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']);
}

I think the issue is with this:

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

is there another way to do this or is this a server problem?

Upvotes: 0

Views: 2346

Answers (1)

cOle2
cOle2

Reputation: 4784

Try using the extractTo() method instead of copy():

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

Upvotes: 1

Related Questions