Posted By

rbl00 on 06/17/09


Tagged

curl php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


PHP Curl


 / Published in: PHP
 

  1. <?php
  2.  
  3. function download($url, $file2save, $directory = 'files')
  4. {
  5.  
  6. $curl = curl_init();
  7.  
  8. if($curl)
  9. {
  10. $fp = fopen( $directory . '/' . $file2save, "w");
  11. if($fp)
  12. {
  13. if( !curl_setopt($curl, CURLOPT_URL, $url) ) return "FAIL: curl_setopt(CURLOPT_URL)";
  14. // uncommment this line below to use http authentication
  15. // if( !curl_setopt($ch, CURLOPT_USERPWD, 'username:password') ) return "FAIL: curl_setopt(CURLOPT_USERPWD)";
  16. if( !curl_setopt($curl, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
  17. if( !curl_setopt($curl, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
  18. if( !curl_exec($curl) ) return "FAIL: curl_exec()";
  19. curl_close($curl);
  20. fclose($fp);
  21. return "It Worked!! : $file2save [$url]";
  22. }
  23. else return "FAIL: fopen()";
  24. }
  25. else return "FAIL: curl_init()";
  26. }
  27.  
  28. // Download from 'example.com' to 'example.txt'
  29. echo download("http://www.symfony-project.org/get/pdf/jobeet-1.2-doctrine-en.pdf", "Jobeet.pdf");

Report this snippet  

You need to login to post a comment.