在CentOs 5.6上使用php脚本强制下载文件
我需要验证用户以查看/下载我站点中的某些文件。我的文件位于一个文件夹中,比如在CentOs 5.6上使用php脚本强制下载文件,php,Php,我需要验证用户以查看/下载我站点中的某些文件。我的文件位于一个文件夹中,比如public\u html/mySiteName/FoldesName(a)/subFolder/sample.(**)。我使用.htaccess检查用户是否经过身份验证 我的.htaccess代码: RewriteEngine on # Enable rewrite RewriteCond %{REQUEST_FILENAME} \.*pdf$|.*psd$|.*indd$ [NC] RewriteRule
public\u html/mySiteName/FoldesName(a)/subFolder/sample.(**)
。我使用.htaccess
检查用户是否经过身份验证
我的
.htaccess
代码:
RewriteEngine on # Enable rewrite
RewriteCond %{REQUEST_FILENAME} \.*pdf$|.*psd$|.*indd$ [NC]
RewriteRule (.*) http://MySiteIp/admin_panel/auth.php?file=$1 [NC,L]
我的下载脚本如下所示:
if($authed){
if(file_exists("../".$_GET['file'])) {
//Create the pointer to our file and open it as read-only binary data
$fp = fopen($file,'rb');
// Send headers telling browser to download our passed data
header("Content-Type: application/force-download");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . filesize($file));
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=\"$file\"");
while (!feof($fp)) {
echo(@fgets($fp, 8192));
}
//Here comes the data
fclose($fp);
//and quit
exit;
} else {
echo "No file exists !!!";
}
}
当我在安装了Xampp服务器的本地机器上测试它时。它起作用了。但是在我的主机上,我收到错误消息“没有文件存在”
请帮我找出我错在哪里…我猜你删除了
回声“here”;模具()在问这个问题之前,请输入code>
总之,本地计算机和web主机上的文件结构是否相同
文件是否实际位于../?也许可以尝试使用文件的完整路径/home/youruser/public\u html/或任何主机的目录结构。就FolderName(A)\SubFolder\而言,您的Web主机看起来是否相似?请尝试使用绝对路径,例如:/home/username/public\u html/FolderName(A)/SubFolder/file
只是一个提示,而不是消毒$\u GET['file']将打开一个巨大的安全漏洞。有人可以传递像“../../somedirectory/somefile.php”这样的值,您的脚本将输出该文件。在查找文件之前,至少应清除$\u GET['file']斜杠。请帮助清理$\u GET['file']。快速清理:$file=!空($\u GET['file'])?(str_replace(“/”,“,$”获取[“文件]):”
然后在文件中使用$file
。\u exists()