pavelazad Posted January 21, 2012 Share Posted January 21, 2012 Hello, I have the following code to force download mp3 file. the code is working fine but its only downloading 799 bytes. I am new in php and not able to figure out the problem. please help me to sort it out. thanks. <?php include "dbconnect.php"; $link=dbconnect(); $cat=$_GET['catagory']; $id=$_GET['id']; $query=("select *from $cat where id='$id'"); $result=mysql_query($query); $row=mysql_fetch_array($result); $file = $row["link"]; header ("Content-type: octet/stream"); header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Length: ".filesize($file)); readfile($file); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2012 Share Posted January 21, 2012 When you open the downloaded file in your programming editor, what do you see? Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/#findComment-1309850 Share on other sites More sharing options...
pavelazad Posted January 21, 2012 Author Share Posted January 21, 2012 when i open the file with editor i see: <br /> <b>Warning</b>: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for /downloads/songs/Bangla/Band/Aurthohin/Aushomapto 2/Alo R Adhar.mp3 in <b>/home/amarlon1/public_html/banglasong/download.php</b> on line <b>20</b><br /> <br /> <b>Warning</b>: Cannot modify header information - headers already sent by (output started at /home/amarlon1/public_html/banglasong/download.php:20) in <b>/home/amarlon1/public_html/banglasong/download.php</b> on line <b>20</b><br /> <br /> <b>Warning</b>: readfile(/downloads/songs/Bangla/Band/Aurthohin/Aushomapto 2/Alo R Adhar.mp3) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>/home/amarlon1/public_html/banglasong/download.php</b> on line <b>22</b><br /> i dont have any idea where is the problem. pls help. Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/#findComment-1309856 Share on other sites More sharing options...
Psycho Posted January 21, 2012 Share Posted January 21, 2012 The file contents you just showed is a clear explanation of the problem. You are NOT downloading the file. Instead, the download contains an error that was generated when trying to send the file to the user. There is an error generated when attempting to run filesize() and when trying to run readfile(). You can ignore the header error - that's only because teh first error sent output tot he browser. My guess is your query is failing because of the '*' being right next to the 'FROM'. But, you shouldn't be using '*' anyway since you don't need all the fields Edit: there are several other issues as well. You are not escaping the user input which could lead to SQL Injection attacks. I assume that ID should be in INT value. Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/#findComment-1309867 Share on other sites More sharing options...
Psycho Posted January 21, 2012 Share Posted January 21, 2012 Here is a revision of your script that corrects the query and adds some security and debugging code <?php include ("dbconnect.php"); $link = dbconnect(); $cat = mysql_real_escape_string($_GET['catagory']); $id = intval($_GET['id']); //Create/Run query $query = "SELECT `link` FROM `{$cat}` WHERE `id`='{$id}' LIMIT 1"; $result = mysql_query($query) or die(mysql_error()); //Validate/Process results if(!mysql_num_rows()) { die("No results for id '{$id}'"); } $file = mysql_result($result, 0); if(!is_file($file)) { die("Result '{$file}' was not a file"); } $basename = basename($file); $filesize = filesize($file); //Output results header("Content-type: octet/stream"); header("Content-Disposition: attachment; filename='{$basename}';"); header("Content-Length: {$filesize}"); readfile($file); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/#findComment-1309872 Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2012 Share Posted January 22, 2012 The path/filename you have stored in the database is apparently relative to your document_root folder or perhaps even your banglasong folder. When used directly as the path/filename, the leading slash / refers to the root of the current hard disk. You would either need to make that into an absolute file system path or a path relative to the download.php script. To use an absolute file system path - $file = $_SERVER['DOCUMENT_ROOT'] . $file; // if the stored path/filename starts at your document_root folder $file = $_SERVER['DOCUMENT_ROOT'] . '/banglasong' . $file; // if the stored path/filename starts at your banglasong folder To use a path relative to folder that the download.php file is in - $file = '.' . $file; Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/#findComment-1309949 Share on other sites More sharing options...
pavelazad Posted January 22, 2012 Author Share Posted January 22, 2012 its working now. you guys are great. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/255477-force-download-mp3-not-downloading-full-filepls-help/#findComment-1310054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.