I had a download page named download.php. Here's source: <?php define('WEBCUAEM',true); include('includes/config.php'); include('includes/functions.php'); $id = addslashes($_GET['id']); if(!is_numeric($id)) { $id=''; } $r = $mysql->fetch_array($mysql->query("SELECT tensoft, link FROM ".$tb_prefix."data WHERE soft_id = '".$id."'")); $link = $r['link']; //do download header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-type: application/force-download"); header("Content-Disposition: attachment; filename=\"".basename($link)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($link)); readfile($link); exit(); ?> PHP: But when call a download link mysite.com/download.php?id=1 It doesnt download the file (when i echo the link, it show the link is correct - http://mylink.com/myfile.mp3 - 4Mb) The link downloaded also had myfile.mp3 but 0Kb I think problem is readfile() funtion (becoz of my link and my site are different host, not on the same server, but dont know how to get this file to download.
Do you really want to download the file to your webserver server first? Why not just direct them to the file on the external server eg: header ("Location: ".$link);
I using it to hide the real link. So when use download a file from server, it only display the download link like http://mysite.com/download/id (i used mod rewrite to rewrite it) I think have some problem here. First is if the link have space. Like this: link.com/my file.mp3. So when download, it dont work. Second, when file have some strange characters like @, & , -, .... It's error too. The file above only work fine if the link have no special character (only abc, number and _ character are fine) Do you have any solutions for this? Thanks for your reply
To solve the problem with strange characters, try urlencode() on the filename portion of the link whenever you use a command that needs to access the file. Also, remove the filesize() line and the Content-Length line, as this is either not reliable, or in some cases might require your server to download the file twice -- which may cause problems. Beyond that, What version of PHP are you using? Do you know if fopen wrappers is enabled? Depending on your memory settings, the file might be taking up too much RAM even if fopen wrappers is enabled. Consider using file_get_contents instead, with a looping through fwrite() that checks whether the connection with the client has been broken.
Ok, then i tried this (i think you mean url decode - not encode) <?php define('WEBCUAEM',true); include('includes/config.php'); include('includes/functions.php'); $id = addslashes($_GET['id']); if(!is_numeric($id)) { $id=''; } $r = $mysql->fetch_array($mysql->query("SELECT tensoft, link FROM ".$tb_prefix."data WHERE soft_id = '".$id."'")); $link = $r['link']; $l = urldecode($link); //do download header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-type: application/force-download"); header("Content-Disposition: attachment; filename=\"".basename($l)."\";" ); header("Content-Transfer-Encoding: binary"); //header("Content-Length: ".filesize($l)); readfile($l); exit(); ?> PHP: But still error when download - It download file with correct name but always 0Kb I'm using PHP 5.2.8 Do you mean allow_url_fopen? It's On Could you example for me about file_get_contents, please
You must use urlencode for the querystring portion (the parts that follow the server name) and not urldecode. This is important when there might be spaces in the URL / URI. You might get better results with: $l=urlencode($link) $myfile=file_get_contents($l);
I dont understand what do you mean? Searched for ob_flush() funtion but nothing to use in this case (or i dont know) Like i said, the $link here is not hosted in same server. $link will return a link like this: http://mysite.com/myfolder/mylink.mp3 (mylink.mp3 may be these: 3, my-link.mp3, my_link.mp3, my link.mp3, my & link.mp3 ....) So for example, if my link is: http://mysite.com/myfolder/my & link.mp3 Then when use urldecode, it will return a link like this: http://mysite.com/myfolder/my &link.mp3 Then i will lose 2 times BW when i think file_get_contents will download from my http://mysite.com/myfolder/my & link.mp3 to my site server (host that script locate) and then user may download. So i lose BW for download from server hosted mp3 file to my script hosted site and then from my script hosted site to user computer. Am i wrong? I want user download direct from mysite.com to their computer.
Could you please tell me some? I searched for ob_flush() funtion but dont see how to use it for a download page.