首頁  >  文章  >  後端開發  >  PHP从网络下载文件

PHP从网络下载文件

WBOY
WBOY原創
2016-07-25 08:46:121039瀏覽
  1. set_time_limit(0);
  2. // Supports all file types
  3. // URL Here:
  4. $url = 'http://somsite.com/some_video.flv';
  5. $pi = pathinfo($url);
  6. $ext = $pi['extension'];
  7. $name = $pi['filename'];
  8. // create a new cURL resource
  9. $ch = curl_init();
  10. // set URL and other appropriate options
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12. curl_setopt($ch, CURLOPT_HEADER, false);
  13. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  15. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  17. // grab URL and pass it to the browser
  18. $opt = curl_exec($ch);
  19. // close cURL resource, and free up system resources
  20. curl_close($ch);
  21. $saveFile = $name.'.'.$ext;
  22. if(preg_match("/[^0-9a-z._-]/i", $saveFile))
  23. $saveFile = md5(microtime(true)).'.'.$ext;
  24. $handle = fopen($saveFile, 'wb');
  25. fwrite($handle, $opt);
  26. fclose($handle);
复制代码

PHP


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn