Saturday, 21 May 2011

Downloading a file from a remote server using cURL

I am currently implementing a new way for themes to be submitted on PS3 Themes. Allowing users to select a remote P3T file and it will be downloaded. There is multiple ways of doing this, but the simplest method is using cURL. The cURL options you will need are CURLOPT_TIMEOUT, CURLOPT_FILE and CURLOPT_FOLLOWLOCATION and you will need the function fopen(). So here goes.
Highlighted using GeSHi
  1. <?php
  2. $f = fopen('theme_name.p3t', 'w+'); // Initiate a file writer for the downloaded file to be written to.
  3. $d = curl_init(''); // initiate cURL on the file to download
  4. // set the appropriate options
  5. curl_setopt($d, CURLOPT_TIMEOUT, 60); // set the time out time for the cURL operation, the bigger the file, the bigger this number should be
  6. curl_setopt($d, CURLOPT_FILE, $f); // this is where you need the file handle, it will put everything straight handle
  7. curl_setopt($d, CURLOPT_FOLLOWLOCATION, true); // this is just so all redirects are actually, otherwise might get the wrong page.
  8. // Execute cURL, this will set it
  9. // close both the file handle and the cURL
  10. fclose($f);
  11. ?>
The code is fairly straight forward, and if not, the comments should help.



  1. Good stuff! Thanks for sharing man

  2. i could definitely use that

  3. Is this just a hobby or are you studying it?
    I learnt a bit of php and phyton (<way easier) but gave up... heh.

  4. Very nifty, I wonder if i'll be able to use it right lol

  5. That might come in handy. Thank you for sharing your knowledge on PHP.

  6. you're all welcome.
    It started out as a hobby, but also studying this stuff now. and I have seen python, I will be starting to learn that soon as well.