php 通过 HTTP 从远程服务器复制图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/909374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:17:29  来源:igfitidea点击:

Copy Image from Remote Server Over HTTP

phpimageimportcurlcopy

提问by GiladG

I am looking for a simple way to import/copy images from remote server to a local folder using PHP. I have no FTP access to the server, but all remote images can be accessed via HTTP (i.e. http://www.mydomain.com/myimage.jpg).

我正在寻找一种使用 PHP 将图像从远程服务器导入/复制到本地文件夹的简单方法。我无法通过 FTP 访问服务器,但可以通过 HTTP(即http://www.mydomain.com/myimage.jpg)访问所有远程图像。

Example use: A user wishes to add an image to his profile. The image already exists on the web and the user provides with a direct URL. I do not wish to hotlink the image but to import and serve from my domain.

使用示例:用户希望将图像添加到他的个人资料中。该图像已存在于网络上,并且用户提供了一个直接 URL。我不想热链接图像,而是从我的域中导入和提供服务。

回答by Ciaran McNulty

If you have PHP5 and the HTTP stream wrapper enabled on your server, it's incredibly simple to copy it to a local file:

如果您的服务器上启用了 PHP5 和 HTTP 流包装器,则将其复制到本地文件非常简单:

copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');

This will take care of any pipelining etc. that's needed. If you need to provide some HTTP parameters there is a third 'stream context' parameter you can provide.

这将处理任何需要的流水线等。如果您需要提供一些 HTTP 参数,则可以提供第三个“流上下文”参数。

回答by dazzafact

use

$imageString = file_get_contents("http://example.com/image.jpg");
$save = file_put_contents('Image/saveto/image.jpg',$imageString);

回答by dazzafact

PHP has a built-in function file_get_contents(), which reads the content of a file into a string.

PHP 有一个内置函数file_get_contents(),它可以将文件的内容读入字符串。

<?php
//Get the file
$content = file_get_contents("http://example.com/image.jpg");

//Store in the filesystem. $fp = fopen("/location/to/save/image.jpg", "w"); fwrite($fp, $content); fclose($fp); ?>

如果您希望将文件存储在数据库中,只需使用 $content 变量,不要将文件保存到磁盘。

回答by Georg Sch?lly

You've got about these four possibilities:

你有这四种可能性:

  • Remote files. This needs allow_url_fopento be enabled in php.ini, but it's the easiest method.

  • Alternatively you could use cURLif your PHP installation supports it. There's even an example.

  • And if you really want to do it manually use the HTTP module.

  • Don't even try to use sockets directly.

  • 远程文件。这需要allow_url_fopen在 php.ini 中启用,但这是最简单的方法。

  • 或者,如果您的 PHP 安装支持,您可以使用cURL。甚至还有一个例子

  • 如果您真的想手动执行此操作,请使用HTTP 模块

  • 甚至不要尝试直接使用套接字

回答by Oli

Here's the most basic way:

这是最基本的方法:

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";

$rfile = fopen($url, "r");
$lfile = fopen($dir . basename($url), "w");

while(!feof($url)) fwrite($lfile, fread($rfile, 1), 1);

fclose($rfile);
fclose($lfile);

But if you're doing lots and lots of this (or your host blocks file access to remote systems), consider using CURL, which is more efficient, mildly faster and available on more shared hosts.

但是如果你做了很多这样的事情(或者你的主机阻止了对远程系统的文件访问),请考虑使用 CURL,它更有效,速度更快,并且可以在更多共享主机上使用。

You can also spoof the user agent to look like a desktop rather than a bot!

您还可以将用户代理伪装成桌面而不是机器人!

$url = "http://other-site/image.png";
$dir = "/my/local/dir/";
$lfile = fopen($dir . basename($url), "w");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);

fclose($lfile);
curl_close($ch);

With both instances, you might want to pass it through GD to make sure it really is an image.

对于这两种情况,您可能希望通过 GD 传递它以确保它确实是一个图像。

回答by PatrikAkerstrand

It's extremely simple using file_get_contents. Just provide the url as the first parameter.

使用file_get_contents非常简单。只需提供 url 作为第一个参数。

回答by ????? ????

make folder and name it foe example download open note pad and insert this code

创建文件夹并将其命名为敌人示例下载打开记事本并插入此代码

only change http://www.google.com/aa.zipto your file and save it to m.php for example

例如,仅将http://www.google.com/aa.zip更改为您的文件并将其保存到 m.php

chamod the php file to 666 and the folder download to 777

chamod php 文件到 666 和文件夹下载到 777

<?php
define('BUFSIZ', 4095);
$url = 'http://www.google.com/aa.zip';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>

finally from your browser enter to these URL http://www.example.com/download/m.php

最后从你的浏览器输入这些 URL http://www.example.com/download/m.php

you will see in download folder the file download from other server

您将在下载文件夹中看到从其他服务器下载的文件

thanks

谢谢

回答by brianegge

Since you've tagged your question 'php', I'll assume your running php on your server. Your best bet is if you control your own web server, then compile cURL into php. This will allow your web server to make requests to other web servers. This can be quite dangerous from a security point of view, so most basic web hosting providers won't have this option enabled.

由于您已将问题标记为“php”,因此我假设您在服务器上运行 php。最好的办法是,如果您控制自己的 Web 服务器,则将 cURL 编译为 php。这将允许您的 Web 服务器向其他 Web 服务器发出请求。从安全的角度来看,这可能非常危险,因此大多数基本的 Web 托管服务提供商都不会启用此选项。

Here's the php man page on using cURL. In the comments you can find an example which downloads and image file.

这是使用 cURLphp 手册页。在评论中,您可以找到下载和图像文件的示例。

If you don't want to use libcurl, you could code something up using fsockopen. This is built into php (but may be disabled on your host), and can directly read and write to sockets. See Examples on the fsockopen man page.

如果您不想使用 libcurl,您可以使用 fsockopen 编写代码。这是内置在 php 中的(但可能在您的主机上被禁用),并且可以直接读取和写入套接字。请参阅fsockopen 手册页上的示例

回答by Peter Stuifzand

Use a GET request to download the image and save it to a web accessible directory on your server.

使用 GET 请求下载图像并将其保存到服务器上的 Web 可访问目录。

As you are using PHP, you can use curlto download files from the other server.

当您使用 PHP 时,您可以使用curl它从其他服务器下载文件。

回答by Marcio Mazzucato

For those who need to preserve the original filename and extension

对于那些需要保留原始文件名和扩展名的人

$origin = 'http://example.com/image.jpg';

$filename = pathinfo($origin, PATHINFO_FILENAME);
$ext = pathinfo($origin, PATHINFO_EXTENSION);

$dest = 'myfolder/' . $filename . '.' . $ext;

copy($origin, $dest);