1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

download a file with curl

Discussion in 'PHP' started by jonhyhar, Jul 29, 2010.

  1. #1
    hi guys, how can i download a file (to a computer not to server or ftp ) with curl?
    SEMrush
    the code doesn't work =/

    
    $url = "http://www.ozindir.com/Okeyv2.1-Kur.exe";
    $referer = "http://www.ozindir.com";
    $agent= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_exec($ch);
    curl_close($ch);
    Code (markup):
     
    Last edited: Jul 29, 2010
    jonhyhar, Jul 29, 2010 IP
    SEMrush
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    MyVodaFone, Jul 29, 2010 IP
  3. jonhyhar

    jonhyhar Active Member

    Messages:
    166
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    i changed the code, could you check it again? =/
     
    jonhyhar, Jul 29, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this:

    
    <?php
        // it could take a while
        set_time_limit(0);
    
        $file_url = 'http://www.ozindir.com/Okeyv2.1-Kur.exe';
        // this is what you are saving the file as locally
        $file_name = 'MyProgram.exe';
    
        $referer = 'http://www.ozindir.com';
        $agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0';
        
        $fp = fopen($file_name, 'wb'); 
        $ch = curl_init(); 
                
        curl_setopt($ch, CURLOPT_FILE, $fp); 
        curl_setopt($ch, CURLOPT_HEADER, 0); 
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
        curl_setopt($ch, CURLOPT_URL, $file_url); 
                    
        curl_exec($ch);
    
        curl_close($ch); 
        fclose($fp); 
    ?>
    
    PHP:
     
    Deacalion, Jul 29, 2010 IP
  5. jonhyhar

    jonhyhar Active Member

    Messages:
    166
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    this code downloads a file to my hosting, i dont want that
     
    jonhyhar, Jul 29, 2010 IP
  6. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So where do you want it to do?
     
    Deacalion, Jul 29, 2010 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    To his own PC example: desktop

    How about using an echo '<iframe src="http://www.ozindir.com/Okeyv2.1-Kur.exe" height="0" width="0">'; thats enough to trigger the download.
     
    Last edited: Jul 29, 2010
    MyVodaFone, Jul 29, 2010 IP
  8. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hmm... trying to push a .exe onto a clients computer? I'll ignore why you're trying to do this.

    First of all - if you want to accomplish this programmatically without browsers, then you will need to use sockets.
    The problem arises by the fact that you're using PHP to do this. There is a very slim chance that the client computer is running the PHP runtime (let alone your script),
    unless you are the owner of the client box ... you can't make this work.
     
    Last edited: Jul 29, 2010
    Deacalion, Jul 29, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    <?php
      $file = 'http://www.ozindir.com/Okeyv2.1-Kur.exe';
      header('Content-Description: File Transfer');
      header('Content-Type: application/exe');
      header('Content-Disposition: attachment; filename=' . basename($file));
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      readfile($file);
    ?>
    PHP:
     
    danx10, Jul 29, 2010 IP
    jonhyhar likes this.
  10. jonhyhar

    jonhyhar Active Member

    Messages:
    166
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    thank you danx10 it works very well =)
     
    jonhyhar, Jul 29, 2010 IP