gpt4 book ai didi

PHP 读取文件返回数据

转载 作者:行者123 更新时间:2023-12-05 07:59:04 25 4
gpt4 key购买 nike

(我关于堆栈的第一个问题,对于错误和/或不准确之处深表歉意...)

所以:我开发了一个 Wordpress 主题,我希望我的用户下载帖子的附件。所以我创建了一个 Zip 存档并使用了一个 readfile()。该函数通过链接调用并使用 $_GET['zip'] 测试。

问题:readfile() 返回(我的意思是显示)这样的二进制字符(我在这里尝试下载一个测试 .png 文件):�PNG IHDRp� r*� pHYs� OiCCPPhotoshop ICC 配置文件x˝SgTS�=����BK���KoR并且没有下载任何内容。

这是我的代码(读取文件部分):

$zip = new ZipArchive();
if ($zip->open($file_path.$archive_file_name, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $file){
$zip->addFile($file,$file);
}
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($file_path.$file).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path.$file));
ob_clean();
flush();
readfile($file_path.'screenshot.png');

我不知道去哪里搜索,这是我第一次使用 zip archive 和 readfile()...

感谢轨道(?我们这么说吗?)!

最佳答案

我不知 Prop 体怎么做,但现在可以了。我在函数的开头添加了一个 ob_start() 。感谢支持!

整个函数:

if (!function_exists('zipThoseFiles')) {
ob_start();
function zipThoseFiles($id){
if (!function_exists('zipFilesDownload')) {
function zipFilesDownload($file_names,$archive_file_name,$file_path){
$zip = new ZipArchive();
if ($zip->open($file_path.$archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $file){
$zip->addFile($file,$file);
}
$zip->close();

header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($file_path.$archive_file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path.$archive_file_name));
ob_end_clean();
flush();
readfile($file_path.$archive_file_name);
unlink($file_path.$archive_file_name);
exit;
}
}
$fileNames = array();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$fileNames[] = get_attached_file( $attachment->ID );
}
}
$zip_file_name = 'mediasArchive.zip';
$file_path = dirname(__FILE__).'/';
zipFilesDownload($fileNames,$zip_file_name,$file_path);
}
}

关于PHP 读取文件返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com