header() funtion - what am i wrong?

Discussion in 'Programming' started by huyhoa, Mar 23, 2009.

  1. #1
    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.
     
    huyhoa, Mar 23, 2009 IP
  2. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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);
     
    joep1978, Mar 23, 2009 IP
  3. huyhoa

    huyhoa Active Member

    Messages:
    214
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3

    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 :)
     
    huyhoa, Mar 24, 2009 IP
  4. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    centralb, Mar 24, 2009 IP
  5. huyhoa

    huyhoa Active Member

    Messages:
    214
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5

    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
     
    huyhoa, Mar 24, 2009 IP
  6. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #6
    search for buffers example
    ob_flush() and other functions
    it might solve your issues
     
    crivion, Mar 24, 2009 IP
  7. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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);
     
    centralb, Mar 24, 2009 IP
  8. huyhoa

    huyhoa Active Member

    Messages:
    214
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    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 &amp;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.
     
    huyhoa, Mar 24, 2009 IP
  9. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #9
    at least you have tried to use ob_flush() ? dont be lazy and expect code already done by someone
     
    crivion, Mar 25, 2009 IP
  10. huyhoa

    huyhoa Active Member

    Messages:
    214
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    Could you please tell me some?
    I searched for ob_flush() funtion but dont see how to use it for a download page. :(
     
    huyhoa, Mar 25, 2009 IP