【问题标题】:php file browser, can browse but can't download files in Chrome...ok in IE (Windows - php code)php 文件浏览器,可以浏览但不能在 Chrome 中下载文件...在 IE 中可以(Windows - php 代码)
【发布时间】:2023-03-05 02:37:01
【问题描述】:

下面是从用 PHP 编写的文件浏览器中提取的一些代码。

实际上,文件浏览和其他所有功能都可以,但是在最近的 Google Chrome 更新之后,下载将无法仅在该浏览器上工作(我还没有测试过 firefox)。 我在其他地方搜索过,但没有发现任何帮助。有趣的是,下载在 IE 中运行良好,并且在最近的 chrome 更新之前运行。

也许我应该知道一些相关的标准变化?

在代码的相关部分下方。

global $_ERROR;
    global $_START_TIME;
    // If user click the download link
    if(isset($_GET['filename']))
    {
        // The directory of downloadable files
        // This directory should be unaccessible from web
        $file_dir="C:/Directory/".$this->location->getDir(false, true, false, 0);

        // Replace the slash and backslash character with empty string
        // The slash and backslash character can be dangerous
        $file_name=str_replace("/", "", $_GET['filename']);
        $file_name=str_replace("\\", "", $file_name);

        // If the requested file exists
        if(file_exists($file_dir.$file_name))
        {
            // Get the file size
            $file_size=filesize($file_dir.$file_name);
            // Open the file
            $fh=fopen($file_dir.$file_name, "r");

            // Download speed in KB/s
            $speed=2048;

            // Initialize the range of bytes to be transferred
            $start=0;
            $end=$file_size-1;

            // Check HTTP_RANGE variable
            if(isset($_SERVER['HTTP_RANGE']) && preg_match('/^bytes=(\d+)-(\d*)/', $_SERVER['HTTP_RANGE'], $arr))
            {
                // Starting byte
                $start=$arr[1];
                if($arr[2])
                {
                    // Ending byte
                    $end=$arr[2];
                }
            }   

            // Check if starting and ending byte is valid
            if($start>$end || $start>=$file_size)
            {
                header("HTTP/1.1 416 Requested Range Not Satisfiable");
                header("Content-Length: 0");
            }
            else
            {
                // For the first time download
                if($start==0 && $end==$file_size)
                {
                    // Send HTTP OK header
                    header("HTTP/1.1 200 OK");
                }
                else
                {
                    // For resume download
                    // Send Partial Content header
                    header("HTTP/1.1 206 Partial Content");
                    // Send Content-Range header
                    header("Content-Range: bytes ".$start."-".$end."/".$file_size);
                }

                // Bytes left
                $left=$end-$start+1;    

                // Send the other headers
                header("Content-Type: application/octet-stream ");
                header("Accept-Ranges: bytes");
                // Content length should be the bytes left
                header("Content-Length: ".$left);
                header("Content-Disposition: attachment; filename=".$file_name);

                // Read file from the given starting bytes
                fseek($fh, $start);
                // Loop while there are bytes left
                while($left>0)
                {
                    // Bytes to be transferred
                    // according to the defined speed
                    $bytes=$speed*1024;
                    // Read file per size
                    echo fread($fh, $bytes);
                    // Flush the content to client
                    flush();
                    // Substract bytes left with the tranferred bytes
                    $left-=$bytes;
                    // Delay for 1 second
                    sleep(1);
                }
            }
            fclose($fh);
        }
        else
        {
            // If the requested file is not exist
            // Display error message
            $_ERROR = "File not found!";
        }
    }

【问题讨论】:

  • 浏览器的输出是什么?可能重复 - stackoverflow.com/questions/8485886/…
  • 没有输出,如上所述,这是在更新当前浏览器后发生的事情,在旧的 chrome 或平台上它可以正常工作。这就是为什么我真的不知道如何让它再次工作。
  • 你检查了另一个问题?这发生在每种文件类型上还是仅在某些特定类型上?
  • 只要我使用的是谷歌浏览器,任何文件都会发生这种情况。这真的很奇怪。无论如何,我认为这与 chrome 在调试模式下发出的警告有关:主线程上的同步 XMLHttpRequest 已被弃用,因为它对最终用户的体验产生不利影响。如需更多帮助,请查看xhr.spec.whatwg.org
  • 我不认为这是一个问题(但您可能想要包含 ajax 调用)。我从未与HTTP_RANGEAccept-Ranges 合作过。白页的来源是什么?并在 chrome 开发工具中检查收到的标头

标签: php html google-chrome internet-explorer


【解决方案1】:

这个相关的代码部分将修复我自己的答案,这是微不足道的,一个简单的错误直到最近更新一个浏览器才注意到:

修改

 // For the first time download
            if($start==0 && $end==$file_size)
            {
                // Send HTTP OK header
                header("HTTP/1.1 200 OK");
            }

进入

 // For the first time download
            if($start==0 && ($end+1)==$file_size)
            {
                // Send HTTP OK header
                header("HTTP/1.1 200 OK");
            }

【讨论】:

    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2014-11-18
    相关资源
    最近更新 更多