丁目不想要下载PHP文件


Chome undesirably downloads PHP files

点击网页上的链接时,Chrome 会下载一个 PHP 文件。内容是一些与我无关的XML。很少见,我宁愿Chrome在同样的情况下表现得像IE,那就是打开Java应用程序。

根据之前对这个主题的研究,请允许我抢占可能的答案:

  1. 我没有互联网下载管理器。

  2. 我已经尝试从一开始就清除所有缓存的数据。

作为比较,Firefox 会提示我使用 Java Web Starter 保存或打开文件;选择后者会导致所需的行为。

我已经读过一些关于安装 Xamp 或 Wamp 的东西 - 我真的不明白这些是什么,也不愿意安装这些。除了学习如何安抚Chrome之外,我还想了解Chrome正在尝试做什么,以及为什么当IE和Firefox能够这样做时它不起作用。在这方面,对php文件的一般解释,它们与Java和/或Java Web Starter的关系是值得赞赏的。

虽然这看起来更适合超级用户,但我会为此付出两分钱。我认为这与Chrome去年放弃NPAPI支持有关。

详细地说,Java浏览器插件依赖于NPAPI,Chrome放弃了对启动版本45的支持。甲骨文似乎并没有研究如何解决这个问题,相反,他们推动用户寻找仍然支持NPAPI(即Firefox,Safari)的替代方案。

在这种情况下,当Chrome加载有问题的页面时,它的行为就像没有安装Java一样(因为它没有办法打开Java应用程序)。

参考文献/延伸阅读:

  • 如何在我的 Web 浏览器中启用 Java?
  • Java 和 Google Chrome 浏览器
  • 我的 Java 小程序会在 Chrome 45 上运行吗?
剪切

并粘贴此PHP,并将其保存为名为download.php的php文件。然后在要下载具有文件名的文件时调用它。例如:

下载.php?文件=名称你的文件这里

这将强制下载文件而不是打开文件。它还可以处理大文件大小。

<?php
$file = $_REQUEST['file'];
$filename = $file;
  define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk
  // Read a file and display its content chunk by chunk
  function readfile_chunked($filename, $retbytes = TRUE) {
    $buffer = '';
    $cnt =0;
    // $handle = fopen($filename, 'rb');
    $handle = fopen($filename, 'rb');
    if ($handle === false) {
      return false;
    }
    while (!feof($handle)) {
      $buffer = fread($handle, CHUNK_SIZE);
      echo $buffer;
      ob_flush();
      flush();
      if ($retbytes) {
        $cnt += strlen($buffer);
      }
    }
    $status = fclose($handle);
    if ($retbytes && $status) {
      return $cnt; 
    }
    return $status;
  }

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile_chunked($filename);
exit(); 
?>