WordPress,Zip和下载在一个文件夹中有效,而在另一个文件夹中无效

时间:2018-10-09 12:38:43

标签: php wordpress ziparchive

I am trying to zip and download files (pdf) in wordpress. It works fine in the root directory. But not working inside wp-content>themes>functions.php. It gets downloaded and on opening, it shows **archive is either unknown format or damaged**. Permission given 777.  Help me please

代码:(这在根目录的文件夹中有效) 在foreach循环中,我得到了$ file。但是file_get_contents不起作用

$i = 0;
$files[$i]['download'] = 'http://live.ifireup.com/wp-content/uploads/signed_forms/signed_form_4787.pdf';
$files[$i]['name'] = "signed_form_" . $i . date('YmdHis') . ".pdf";

$i = 1;
$files[$i]['download'] = 'http://live.ifireup.com/wp-content/uploads/signed_forms/signed_form_4787.pdf';
$files[$i]['name'] = "signed_form1_" . $i . date('YmdHis') . ".pdf";

$tmpFile = tempnam('/tmp', '');

$zip = new ZipArchive;
$zip->open($tmpFile, ZipArchive::CREATE);
foreach ($files as $file) {
    // download file
    $fileContent = file_get_contents($file['download']);

    $zip->addFromString(basename($file['name']), $fileContent);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=SignedForms.zip');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);

unlink($tmpFile);
exit;

0 个答案:

没有答案