【发布时间】:2013-12-23 14:57:26
【问题描述】:
我有一个带有图片库和自定义构建灯箱的网站。 我正在为访问者提供下载当前显示的图像。 url 参数已正确传递,但不知何故,该脚本仅适用于访问者下载的第一个图像。之后我得到错误
警告:fopen() [function.fopen]:无法访问。 (路径)
所有文件都存在,权限设置正确。
下载文件的代码是:
<?php
if( isset( $_GET[ 'file' ] ) && basename( $_GET[ 'file' ] ) == $_GET[ 'file' ] ) {
$file = $_GET[ 'file' ];
$path = $_GET[ 'path' ];
$fullpath = realpath( $_SERVER[ 'DOCUMENT_ROOT' ] ) . '/' . $path . '/' . $file;
if( $fd = fopen ( $fullpath , 'r' ) ) {
$fsize = filesize( $fullpath );
$path_parts = pathinfo( $fullpath );
$ext = strtolower( $path_parts[ 'extension' ] );
switch( $ext ) {
case 'pdf':
$ctype = 'application/pdf';
break;
case 'exe':
$ctype = 'application/octet-stream';
break;
case 'zip':
$ctype = 'application/zip';
break;
case 'doc':
$ctype = 'application/msword';
break;
case 'gif':
$ctype = 'image/gif';
break;
case 'png':
$ctype = 'image/png';
break;
case 'jpeg':
$ctype = 'image/jpg';
break;
case 'jpg':
$ctype = 'image/jpg';
break;
default:
$ctype = 'application/force-download';
}
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private' , false );
header( 'Content-type: ' . $ctype );
header( 'Content-Disposition: attachment; filename=\'' . $file . '\'' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-length: ' . $fsize );
header( 'Cache-control: public' );
while( !feof( $fd ) ) {
echo fread( $fd , 1024*8 );
flush();
}
}
fclose ( $fd );
}
?>
如果我尝试使用提供的文件名和路径直接在浏览器中打开文件,它会正确显示图像。
【问题讨论】:
-
检查您的文件是否有必要的权限。
-
据我所见,您目前正在实施一个后门程序,允许具有极少黑客技能的用户基本上下载您的 webroot 中的任何文件。在使用它来提供文件之前,您确实必须对输入进行清理。
-
嗯。老实说,我从第三方网站获得了这个脚本。主要是懒得自己写。您是否有一些线索可以找到更好(也更安全)的此类脚本版本?