簡體   English   中英

在php上下載時重命名zip內的文件

[英]renaming files inside zip while download on php

我有一個問題要告訴我,如何下載zip並使用一些php代碼重命名zip中的文件? 這是我的代碼:

$file_path='../../files_pangkat/';
$file_names[0] = $ck_sk_terakhir[file_pangkat];
$zip = new ZipArchive();

//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    exit("cannot open <$archive_file_name>\n");
}

//add each files of $file_name array to archive
$zip->addFile($file_path.$file_names[0],$file_names[0]);
$zip->renameName($file_names[0], 'SK_PNS_'.$c_bio[nip_baru].'pdf');
echo $file_path.$file_names[0],$file_names[0]."<br />";
$zip->close();

//then send the headers to foce download the zip file 
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$archive_file_name");  
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$archive_file_name");
unlink("$archive_file_name");
exit;

$file_names是我保存在數據庫中的文件的名稱,但是當我使用$zip->renameName() ,它不起作用..

首先將文件添加到zip文件,然后在此之后立即重命名新添加的文件似乎沒有多大意義。

相反,您應該使用所需的名稱添加文件,另請參見addFile()上的手冊。

因此,您應該更改:

$zip->addFile($file_path.$file_names[0],$file_names[0]);
$zip->renameName($file_names[0], 'SK_PNS_'.$c_bio[nip_baru].'pdf');

至:

$zip->addFile($file_path . $file_names[0], 'SK_PNS_' . $c_bio[nip_baru] . 'pdf');

Zip在文件上創建了一個鎖。您無法即時重命名,將其關閉並再次重命名。 試試吧。

$zip->addFile('.....');
....
$zip->close();

$zip->renameName($file_names[0], 'SK_PNS_'.$c_bio[nip_baru].'pdf');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM