欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP从网络下载文件

程序员文章站 2022-04-21 22:31:31
...
  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