Jump to content

force download mp3, not downloading full file.....pls help


pavelazad
 Share

Recommended Posts

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;                 
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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;

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.