本文介绍了如何从外部根目录中的文件访问PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个下载zip文件的代码:
I have a code to download zip files:
$dl_path = './';
$filename = 'favi.zip';
$file = $dl_path.$filename;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/zips');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=$filename ");
header("Content-Transfer-Encoding:binary ");
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
有根目录/public_html
,脚本正在根目录中执行.
there is root directory /public_html
, the script is executing in root directory.
在/
目录中有一个zip文件.
there is zip file in /
directory.
我正在尝试将$dl_path
用作/
,但是它不起作用.
i am trying to use the $dl_path
as /
but it is not working.
请帮助.
推荐答案
$dl_path = __DIR__.'/..'; // parent folder of this script
$filename = 'favi.zip';
$file = $dl_path . DIRECTORY_SEPARATOR . $filename;
// Does the file exist?
if(!is_file($file)){
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
header("Status: 404 Not Found");
echo 'File not found!';
die;
}
// Is it readable?
if(!is_readable($file)){
header("{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden");
header("Status: 403 Forbidden");
echo 'File not accessible!';
die;
}
// We are good to go!
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary ");
header('Content-Length: ' . filesize($file));
while(ob_get_level()) ob_end_clean();
flush();
readfile($file);
die;
^ 尝试此代码.看看是否可行.如果没有:
^ try this code. See if it works. If it doesn't:
- 如果
404
表示找不到该文件. - 如果它是
403
,则表示您无法访问它(权限问题).
- If it
404
's means the file is not found. - If it
403
's it means you can't access it (permissions issue).
这篇关于如何从外部根目录中的文件访问PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!