php下载脚本错误

时间:2015-01-08 19:38:28

标签: php download

我使用此功能制作下载链接:

    <?php    
function downloadFile($fileLocation,$fileName,$maxSpeed = 13,$doStream =
false){
    if (connection_status()!=0) return(false);
    $extension = strtolower(end(explode('.',$fileName)));

    /* List of File Types */
    $fileTypes['swf'] = 'application/x-shockwave-flash';
    $fileTypes['pdf'] = 'application/pdf';
    $fileTypes['exe'] = 'application/octet-stream';
    $fileTypes['zip'] = 'application/zip';
    $fileTypes['doc'] = 'application/msword';
    $fileTypes['xls'] = 'application/vnd.ms-excel';
    $fileTypes['ppt'] = 'application/vnd.ms-powerpoint';
    $fileTypes['gif'] = 'image/gif';
    $fileTypes['png'] = 'image/png';
    $fileTypes['jpeg'] = 'image/jpg';
    $fileTypes['jpg'] = 'image/jpg';
    $fileTypes['rar'] = 'application/rar';    

    $fileTypes['ra'] = 'audio/x-pn-realaudio';
    $fileTypes['ram'] = 'audio/x-pn-realaudio';
    $fileTypes['ogg'] = 'audio/x-pn-realaudio';

    $fileTypes['wav'] = 'video/x-msvideo';
    $fileTypes['wmv'] = 'video/x-msvideo';
    $fileTypes['avi'] = 'video/x-msvideo';
    $fileTypes['asf'] = 'video/x-msvideo';
    $fileTypes['divx'] = 'video/x-msvideo';

    $fileTypes['mp3'] = 'audio/mpeg';
    $fileTypes['mp4'] = 'audio/mpeg';
    $fileTypes['mpeg'] = 'video/mpeg';
    $fileTypes['mpg'] = 'video/mpeg';
    $fileTypes['mpe'] = 'video/mpeg';
    $fileTypes['mov'] = 'video/quicktime';
    $fileTypes['swf'] = 'video/quicktime';
    $fileTypes['3gp'] = 'video/quicktime';
    $fileTypes['m4a'] = 'video/quicktime';
    $fileTypes['aac'] = 'video/quicktime';
    $fileTypes['m3u'] = 'video/quicktime';

    $contentType = $fileTypes[$extension];


    header("Cache-Control: public");
    header("Content-Transfer-Encoding: binary\n");
    header("Content-Type: $contentType");

    $contentDisposition = 'attachment';

    if($doStream == true){
        /* extensions to stream */
        $array_listen = array('mp3','m3u','m4a','mid','ogg','ra','ram','wm',
        'wav','wma','aac','3gp','avi','mov','mp4','mpeg','mpg','swf','wmv','divx','asf');
        if(in_array($extension,$array_listen)){ 
            $contentDisposition = 'inline';
        }
    }

    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
        $fileName= preg_replace('/\./', '%2e', $fileName,
substr_count($fileName, '.') - 1);
        header("Content-Disposition: $contentDisposition; filename=".$fileName);
    } else {
        header("Content-Disposition: $contentDisposition; filename=".$fileName);
    }

    header("Accept-Ranges: bytes");   
    $range = 0;
    $size = filesize($fileLocation);

    if(isset($_SERVER['HTTP_RANGE'])) {
        list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
        str_replace($range, "-", $range);
        $size2=$size-1;
        $new_length=$size-$range;
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: $new_length");
        header("Content-Range: bytes $range$size2/$size");
    } else {
        $size2=$size-1;
        header("Content-Range: bytes 0-$size2/$size");
        header("Content-Length: ".$size);
    }

    if ($size == 0 ) { die('Zero byte file! Aborting download');}
    set_magic_quotes_runtime(0); 
    $fp=fopen("$fileLocation","rb");

    fseek($fp,$range);

    while(!feof($fp) and (connection_status()==0))
    {
        set_time_limit(0);
        print(fread($fp,1024*$maxSpeed));
        flush();
        ob_flush();
        sleep(1);
    }
    fclose($fp);

    return((connection_status()==0) and !connection_aborted());
} 

/* Implementation */
$filename="shopfile.zip";
$location="file/";
downloadFile($location,$filename,900,false); 

?>

但是当它运行时它会得到这个错误: 严格标准:只应在第16行的/home/behzadfar/public_html/shopfile/download.php中通过引用传递变量

警告:无法修改标题信息 - 已在第57行的/home/behzadfar/public_html/shopfile/download.php中发送的标题(由/home/behzadfar/public_html/shopfile/download.php:16开始输出)< / p>

我该如何解决?

3 个答案:

答案 0 :(得分:1)

解决问题: 您必须将实际变量传递给end(),而不是返回数组的函数。所以:

$pieces = explode('.',$fileName);
$extension = strtolower(end($pieces));

PHP's manual end解释了这一点。

<强>摘录

  

此数组通过引用传递,因为它由函数修改。这意味着你必须传递一个实变量,而不是一个返回数组的函数,因为只有实际的变量可以通过引用传递。

没有中间变量的解决方案

如果您不想使用中间变量,请使用array_pop代替end

$extension = strtolower(array_pop(explode('.',$fileName)));

与已经发送的标题相关的错误出现是因为PHP已经必须向您显示页面上的错误(已传递的内容),因此它阻止了自己修改标题的能力(一旦开始交付,您就无法修改HTTP标头内容)。修复严格标准错误将修复标头错误。

答案 1 :(得分:0)

错误来自这一行:

$extension = strtolower(end(explode('.',$fileName)));

end()的参数通过引用传递(请参阅manualmixed end ( array &$array )

因此,您必须将代码更改为:

$array = explode('.',$fileName);
$extension = strtolower(end($array));

再看pathinfo(),这将是一个更清洁的解决方案:

$extension = pathinfo($fileName, PATHINFO_EXTENSION);

如果它已经是标准php函数的一部分,为什么要自己实现这个函数。

答案 2 :(得分:0)

为什么不用http://php.net/manual/en/function.finfo-file.php替换该数组:

$finfo = new finfo(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension

$contentType =  $finfo->file($fileLocation.$filename) ;

你的脚本是以4还是空格开始的?