0

いくつかのdocファイルとpdfファイルを含むzipファイルをphpで作成する必要があります。Webで見つけた関数を使用します:

function create_zip($files = array(),$destination = '',$overwrite = true) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    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;
}

問題は、zip に含めるファイル パスが path/to/file/filename.pdf の場合、zip をダウンロードして開くと、filename.pdf が path/to/file フォルダーに含まれることです。すべてのファイルを zip のルートに配置するにはどうすればよいですか?

4

1 に答える 1

1

の 2 番目のパラメーターを使用してZipArchive::addFile()、ファイルがアーカイブに表示されるファイル名を指定します。basename($file)その有力候補です。

于 2013-07-11T13:11:41.387 に答える