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

标签 php mysql curl

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

我已将 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/

相关文章:

php - SWIG fatal error : can not redeclare class

php - 日历脚本 - 在日期列中映射 ID 和日期

javascript - Google Drive API 使用 curl 调用创建文件而不是文件夹

iphone - Apple 终端无法访问 SSL 站点和神秘的 curl 错误

mysql - SQL 在第一列中使用 IN 并返回第二列

bash - 为什么在传递到 bash 时使用 -Lo- 和 curl?

php - 对 HTTP Post 请求采取操作

php - 什么更消耗资源?每次存储或读取

PHP 标准值为 NULL 并且不会更新

mysql - 是否可以独立连接MySQL行