'PHP Download Files from another Server using File URL
Right now, if I paste the same URL in Chrome's URL Bar, it downloads the file accurately.
But when I add the same URL inside ANCHOR TAG it just reloads the same page. I have tried both _self and _blank values for TARGET Attribute.
Here are the Anchor tags I used:
<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>
NOTE: These anchors are working fine in Microsoft Edge
Also, I tried the different PHP scripts for this and one of them downloads the file but the file is empty. Changing URLs for different types of files including pdf, jpg, png and all of them has filesize of zero.
$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);
I don't know if its a cross-domain issue or just some error that's downloading an empty file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
