首页 > 解决方案 > PHP 使用文件 URL 从另一台服务器下载文件

问题描述

现在,如果我在 Chrome 的 URL 栏中粘贴相同的 URL,它会准确地下载文件。

但是当我在 ANCHOR TAG 中添加相同的 URL 时,它只会重新加载相同的页面。我已经尝试了 TARGET 属性的 _self 和 _blank 值。

这是我使用的锚标签:

<a href="url_to_file" download="link.vpf">Download File</a>
<a href="url_to_file" target="_blank">Download File</a>
<a href="url_to_file" target="_self">Download File</a>

注意: 这些锚点在 Microsoft Edge 中运行良好

此外,我为此尝试了不同的 PHP 脚本,其中一个下载了文件,但文件为空。更改不同类型文件的 URL,包括 pdf、jpg、png 和所有这些文件的文件大小为零。

$file_name  =   basename($vpf_url);
$fn         =   file_put_contents($file_name,file_get_contents($vpf_url));
header("Expires: 0");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/octet-stream");
header('Content-length: '.filesize($file_name));
header('Content-disposition: attachment; filename="'.basename($file_name).'"');
readfile($file_name);

我不知道它是跨域问题还是只是下载空文件的一些错误。

标签: phpgoogle-chrome

解决方案


推荐阅读