PHP文件下载错误


PHP File Download Error

我有一个网站,里面有一个图片库和一个自定义的构建灯箱。我提供访问者下载当前显示的图像。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 );
}
?>

如果我尝试使用提供的文件名和路径直接在浏览器中打开文件,它会正确显示图像。

您的web服务器是否可以访问该目录(在apachewww数据中)?