php下载脚本错误

php下载脚本错误,php,download,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 */

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

    <?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:16)中的/home/behzadfar/public_html/shopfile/download.php发送

如何解决此问题?

要解决此问题,请执行以下操作: 必须将实际变量传递给
end()
,而不是返回数组的函数。因此:

$pieces = explode('.',$fileName);
$extension = strtolower(end($pieces));
对于
end
,这在中进行了说明

摘录

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

不含中间变量的解决方案

如果不想使用中间变量,请使用而不是
end

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

出现与已发送的标题相关的错误是因为PHP必须在页面(已交付内容)上向您显示错误,因此它无法修改标题(一旦开始交付内容,您就无法修改HTTP标题)。修复strict standards错误将修复标头错误。

错误来自以下行:

$extension = strtolower(end(explode('.',$fileName)));
end()
的参数通过引用传递(请参阅:
mixed end(数组和$array)

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

$array = explode('.',$fileName);
$extension = strtolower(end($array));
此外,这将是一个更清洁的解决方案:

$extension = pathinfo($fileName, PATHINFO_EXTENSION);

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


您的脚本是否以4或任何空格开头???

哪一行是原始脚本的第16行?ooooohh:-)您的脚本是否以4或任何空格开头??标题错误是由于严格的标准警告,而不是因为脚本中的任何其他内容。。。
$finfo = new finfo(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension

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