php unzipsteemCreated with Sketch.

in php •  7 years ago 
function unzip($znm)
{
    if (!file_exists($znm)){
        throw new Exception("[$znm] not exists ");
    }
    $dnm = pathinfo($znm, PATHINFO_FILENAME);
    if (file_exists($dnm) && ! is_dir($dnm)){
        throw new Exception("[$dnm] is exists file");
    }
    $zfd = new ZipArchive();
    if ($zfd->open($znm) !== false) {
        $zfd->extractTo($dnm);
        $zfd->close();
    }
    
    $scand1 = array_diff(scandir($dnm), array( '..', '.' ));
    if (count($scand1) != 1) return ;
    foreach ($scand1 as &$f1) { // sub has 1 folder , move sub/* ..
        if (is_dir($f1)){
            $scand2 = array_diff(scandir("$dnm/$f1"), array( '..', '.' ));
            foreach ($scand2 as &$f2) {
                rename("$dnm/$f1/$f2", "$dnm/$f2");
            }
        }
        break;
    }
    rmdir($dnm);
}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!