当前位置:Gxlcms > PHP教程 > PHP从网络下载文件

PHP从网络下载文件

时间:2021-07-01 10:21:17 帮助过:16人阅读

  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

人气教程排行