简体   繁体   中英

File download iframe absolute vs relative address

We are using this code in javascript for downloading files via iframe and it works well:

function initDownload(){
var path = "path/to/song.mp3", name = "song.mp3";
document.getElementById('myiframe').src = "dl.php?path="+path+"&name="+name;
}

<iframe id="myiframe" style="position: absolute; left: -10000px; display: none;" src=""></iframe>
<a href="#" onClick="initDownload(); return false;">download</a> 

Here is dl.php file:

<?php 
header("Content-Type: application/octet-stream");
if(isset($_GET['name'])){
    $name = $_GET['name'];
    header("Content-Disposition: attachment; filename=\"$name\"");
}
if(isset($_GET['path'])){
   $path = str_replace(' ', '%20', $_GET['path']);
   readfile($path);
}
exit;

?>

Song path is set as absolute url and this works well. There have been a case when this havent worked, and after some testing, it turned out that only relative url was accepted (worked) on that particular server. Is there a reason why an absolute url wouldnt worked on some servers, maybe some php setting or something?

You should check, what you are doing in dl.php file with path, here html and javascript will work on all servers the same way. Quite shure the problem is in the way php getting to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM