readfile return 打开流:HTTP 请求失败! HTTP/1.1 403 在 php 中被禁止
readfile return open stream: HTTP request failed! HTTP/1.1 403 Forbidden in php
我已经在 Codeigniter 控制器中创建了一个函数来下载文件,这里是它的代码。
function download_example($file_name) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);
ob_clean();
flush();
readfile(base_url().'download_example/'.$file_name);
}
但是下载后在文件中显示 403 Forbidden error 像这样。
A PHP Error was encountered
Severity: Warning
Message:
readfile(http://example.com/download_example/example.txt): failed to
open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Filename: controllers/controllers.php
Line Number: 371
这不是您的脚本的问题,而是您请求的资源的问题。 Web 服务器正在返回 "forbidden" 状态代码。
可能是它阻止了 PHP 脚本以防止抓取。
您可能应该与远程服务器的管理员联系。
改为:
readfile(dirname(__FILE__).'/download_example/'.$file_name);
如果是本地文件,当前PHP脚本目录与download_example
目录相同。更改以匹配您的系统。还要确保该文件确实存在,并且网络服务器 运行 所在的用户有权访问该文件。
使用 base_url()
可以获得文件的 http 地址,如 http://example.com/download_example/file_name
,但最好使用服务器系统路径,如 C:\path\to\site_dir\download_example_dir\file_name
。
我个人是这样使用的:
dirname($this->input->server('SCRIPT_FILENAME')).'/download_example_dir/file_name'
我已经在 Codeigniter 控制器中创建了一个函数来下载文件,这里是它的代码。
function download_example($file_name) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);
ob_clean();
flush();
readfile(base_url().'download_example/'.$file_name);
}
但是下载后在文件中显示 403 Forbidden error 像这样。
A PHP Error was encounteredSeverity: Warning
Message: readfile(http://example.com/download_example/example.txt): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Filename: controllers/controllers.php
Line Number: 371
这不是您的脚本的问题,而是您请求的资源的问题。 Web 服务器正在返回 "forbidden" 状态代码。
可能是它阻止了 PHP 脚本以防止抓取。
您可能应该与远程服务器的管理员联系。
改为:
readfile(dirname(__FILE__).'/download_example/'.$file_name);
如果是本地文件,当前PHP脚本目录与download_example
目录相同。更改以匹配您的系统。还要确保该文件确实存在,并且网络服务器 运行 所在的用户有权访问该文件。
使用 base_url()
可以获得文件的 http 地址,如 http://example.com/download_example/file_name
,但最好使用服务器系统路径,如 C:\path\to\site_dir\download_example_dir\file_name
。
我个人是这样使用的:
dirname($this->input->server('SCRIPT_FILENAME')).'/download_example_dir/file_name'