php php创建没有zip文件路径的zip文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3993105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:40:07  来源:igfitidea点击:

php creating zips without path to files inside the zip

phpzipdirectory

提问by SoulieBaby

I'm trying to use php to create a zip file (which it does - taken from this page - http://davidwalsh.name/create-zip-php), however inside the zip file are all of the folder names to the file itself.

我正在尝试使用 php 创建一个 zip 文件(它确实这样做了 - 取自此页面 - http://davidwalsh.name/create-zip-php),但是在 zip 文件中包含所有文件夹名称到文件本身。

Is it possible to just have the file inside the zip minus all the folders?

是否可以只将 zip 中的文件减去所有文件夹?

Here's my code:

这是我的代码:

function create_zip($files = array(), $destination = '', $overwrite = true) {

    if(file_exists($destination) && !$overwrite) { return false; };
    $valid_files = array();
    if(is_array($files)) {
        foreach($files as $file) { 
            if(file_exists($file)) { 
                $valid_files[] = $file;
            };
        };
    };
    if(count($valid_files)) { 
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
            return false;
        };
        foreach($valid_files as $file) { 
            $zip->addFile($file,$file);
        };
        $zip->close();
        return file_exists($destination);
    } else {
        return false;
    };

};


$files_to_zip = array('/media/138/file_01.jpg','/media/138/file_01.jpg','/media/138/file_01.jpg');

$result = create_zip($files_to_zip,'/...full_site_path.../downloads/138/138_files.zip');

回答by Nathan Osman

The problem here is that $zip->addFileis being passed the same two parameters.

这里的问题是$zip->addFile传递了相同的两个参数。

According to the documentation:

根据文档

bool ZipArchive::addFile( string $filename[, string $localname] )

filename
The path to the file to add.

localname
local name inside ZIP archive.

bool ZipArchive:: addFile( string $filename[, string $localname] )

filename
要添加的文件的路径。

localname
ZIP 存档中的本地名称。

This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

这意味着第一个参数是文件系统中实际文件的路径,第二个参数是文件在存档中的路径和文件名。

When you supply the second parameter, you'll want to strip the path from it when adding it to the zip archive. For example, on Unix-based systems this would look like:

当您提供第二个参数时,您需要在将其添加到 zip 存档时从中删除路径。例如,在基于 Unix 的系统上,这看起来像:

$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);

回答by Carlos évora

I think a better option would be:

我认为更好的选择是:

$zip->addFile($file,basename($file));

Which simply extracts the filename from the path.

它只是从路径中提取文件名。

回答by FreeSoftwareServers

This is just another method that I found that worked for me

这只是我发现对我有用的另一种方法

$zipname = 'file.zip';
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
$zip->close();
header('Content-disposition: attachment; filename='.$zipname);
header('Content-type: application/zip');
readfile($tmp_file);