gpt4 book ai didi

php - 使用 cURL 将外部文件保存到我的服务器

转载 作者:可可西里 更新时间:2023-11-01 07:01:41 24 4
gpt4 key购买 nike

我有一个网站来展示开源电影和视频。

我已将 url 保存在 mysql 中,并将视频和图像链接到内容服务器。

但是用户提示网站速度慢,因为图像是从外部获取的,而且大多数时候 Internet Explorer 甚至不显示图像。

我刚刚了解cURL,想将图片和视频保存到我自己的服务器上,并提供原始网站的镜像。

我在很多地方都有“curl -O ('') ;”语法来完成任务,但不知道如何在我的 php 脚本中使用它。

简而言之:我已经有了在 mysql 中保存 url 的表单。我希望它也能将保存文件保存到我的网络服务器上的一个目录,并将文件路径保存到 mysql 中的另一列。

欢迎任何形式的帮助。提前致谢

最佳答案

$local_file = "/tmp/filename.flv";//This is the file where we save the information
$remote_file = "http://www.test.com/filename.flv"; //Here is the file we are downloading


$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);

我决定在将近 7 年后更新这个答案。
对于那些为远程主机启用了 copy() 的用户,您可以简单地使用:

copy("http://www.test.com/filename.flv", "/some/local/path/filename.flv");

关于php - 使用 cURL 将外部文件保存到我的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7100392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com