Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. // Function to download a file (needed in one of the programming missions)
  2. function htsGetFile($url, $filename) {
  3. $cookiefile = "cookie.txt";
  4.  
  5. $ch = curl_init();
  6. curl_setopt($ch, CURLOPT_URL, $url);
  7. curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0');
  8.  
  9. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  10. curl_setopt($ch, CURLOPT_REFERER, $url);
  11. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  12.  
  13. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); // Cookiejar on creation, cookiefile on use
  14.  
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We need tot get the data back.
  16. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  17.  
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Will not check the ssl sertification.
  19.  
  20. $rawdata = curl_exec($ch);
  21.  
  22. if(file_exists($filename)){
  23. unlink($filename);
  24. }
  25.  
  26. $fp = fopen($filename, 'wb');
  27. fwrite($fp, $rawdata);
  28. fclose($fp);
  29.  
  30. if(curl_errno($ch)) {
  31. return false;
  32. } else {
  33. return $rawdata;
  34. }
  35. curl_close ($ch);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. $username = "*******";
  42. $password = "*******";
  43.  
  44. $url = "https://www.domain/path/";
  45. $file = "download.png";
  46.  
  47. if (htsLogin($username, $password))
  48. if (htsGetHTML($url) !== false)
  49. if (htsGetFile($url, $file) !== false)
  50. $img = htsGetFile($url, $file);
  51. else
  52. echo 'Getting file went wrong.';
  53. else
  54. echo 'Getting page went wrong.';
  55. else
  56. echo 'Can't login.';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement