gpt4 book ai didi

PHP- 从 URL 强制下载文件

转载 作者:行者123 更新时间:2023-11-29 03:24:16 25 4
gpt4 key购买 nike

我有问题。我创建了一个带有上传文件的网页,我有数据库 (MySQL),其中包含正确的文件 URL(文件路径)(在数据库中只是文件的 URL,文件在服务器上的文件夹中)。现在我尝试编写/搜索 php 脚本来下载上传的文件。 All at web 运行良好,但此下载脚本不起作用。我读过,我认为对我来说最好的方法是用“标题”下载,但我试过了,什么也没有。文件已下载到我们的磁盘,名称正确,文件扩展名正确,文件在正确的程序中打开,但如果文件名称中有字符(';',':'等)文件下载时没有这些字符并且不正确(缺少扩展名,坏名)。第二个问题:所有下载的文件都是空的(0 b 大小),一切正常但它们是空的,一些提示?在此先感谢您的帮助

我的错误下载代码:

<?php
$data=date('Y-m-d_H:i:s');
$nazwa=$_POST['downtitle'];
$urlek=$_POST['downurl'];
$extrozsz=$_POST['downext'];
$filePath = $urlek;
$fileName = $nazwa.$data.".".$extrozsz;

//------------------------------------------------------------------------

// $fd = fopen($filePath,"r");
// $size = filesize($filePath);
// $contents = fread($fd, filesize($filePath));

// fclose($fd);

// header("Content-Type: application/octet-stream");
// header("Content-Length: $size;");
// header("Content-Disposition: attachment; filename=$fileName");

// echo $contents;

//----------------------------------------------------------------------


// set_time_limit(0);
// while( $urlek = $response->fetch() )
// {
// $my_pliki = file_get_contents("$urlek");
// $my_file = fopen("$urlek","w+");
// fwrite($my_file,$my_pliki);
// fclose($my_file);
// }

//----------------------------------------------------------------------------

$quoted = $filePath;
$size = filesize($file);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);

?>

最佳答案

文件名参数有一些限制,在 RFC6266 中有详细描述。

因此,当您想要使用 ISO-8859-1 字符集中不存在的字符时,您需要使用 filename* header ,或者您需要从文件名中去除/替换这些字符.可以找到很好的解决方案 here

关于PHP- 从 URL 强制下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38892806/

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