English 中文(简体)
积的档案被摘取,以扎根。
原标题:Extract files in a zip to root of a folder?
  • 时间:2011-07-13 16:44:14
  •  标签:
  • php
  • zip

我有一台齐普文件上载到服务器,用于自动提取。

锡普卷宗建筑也一样:

/zip_file.zip/folder1/image1.jpg
/zip_file.zip/folder1/image2.jpg
/zip_file.zip/folder1/image3.jpg

目前,我有这一职能,可以提取所有可延伸部分的卷宗:

$zip = new ZipArchive();
    if( $zip->open($file_path) ){
        $files = array();
        for( $i = 0; $i < $zip->numFiles; $i++){
            $entry = $zip->statIndex($i);
            // is it an image?  
            if( $entry[ size ] > 0 && preg_match( #.(jpg)$#i , $entry[ name ] ) ){
                $f_extract = $zip->getNameIndex($i);
                $files[] = $f_extract;
            }
        }
        if ($zip->extractTo($dir_name, $files) === TRUE) {
        } else {
            return FALSE;
        }

        $zip->close();
    }

但是,通过使用<代码>推广功能 至,它将摘录如下:

/myFolder/folder1/image1.jpg
/myFolder/folder1/image2.jpg
/myFolder/folder1/image3.jpg

是否有办法将文件夹在文件夹1中,以图人之根?

理想:

/myFolder/image1.jpg
/myFolder/image2.jpg
/myFolder/image3.jpg

PS:在冲突档案中,我只需要提取或超过档案。

问题回答

Use this little code snippet instead. It removes the folder structure in front of the filename for each file so that the whole content of the archive is basically extracted to one folder.

<?php
$path = "zip_file.zip";

$zip = new ZipArchive();
if ($zip->open($path) === true) {
    for($i = 0; $i < $zip->numFiles; $i++) {
        $filename = $zip->getNameIndex($i);
        $fileinfo = pathinfo($filename);
        copy("zip://".$path."#".$filename, "/myDestFolder/".$fileinfo[ basename ]);
    }                  
    $zip->close();                  
}
?>

这里:(一) 努力管理一切事项。

$zip = new ZipArchive();
if( $zip->open($file_path) ){
    $files = array();
    for( $i = 0; $i < $zip->numFiles; $i++){
        $entry = $zip->statIndex($i);
        // is it an image?  
        if( $entry[ size ] > 0 && preg_match( #.(jpg)$#i , $entry[ name ] ) ){
            $f_extract = $zip->getNameIndex($i);
            $files[] = $f_extract; /* you man want to keep this array (use it to show result or something else) */

            if ($zip->extractTo($dir_name, $f_extract) === TRUE) {
                $solid_name = basename($f_extract);
                if(strpos($f_extract, "/")) // make sure zipped file is in a directory
                {
                    if($dir_name{strlen($dir_name)-1} == "/") $dir_name = substr($dir_name, 0, strlen($dir_name)-1); // to prevent error if $dir_name have slash in end of it
                    if(!file_exists($dir_name."/".$solid_name)) // you said you don t want to replace existed file
                        copy($dir_name."/".$f_extract, $dir_name."/".$solid_name); // taking file back to where you need [$dir_name]
                    unlink($dir_name."/".$f_extract); // [removing old file]
                    rmdir(str_replace($solid_name, "", $dir_name."/".$f_extract)); // [removing directory of it]
                }
            } else {
                 echo("error on export<br />
");
            }
        }
    }


    $zip->close();
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签