Gil*_*adG 79 php import curl copy image
我正在寻找一种使用PHP将图像从远程服务器导入/复制到本地文件夹的简单方法.我没有FTP访问服务器,但所有远程图像都可以通过HTTP访问(即http://www.mydomain.com/myimage.jpg).
使用示例:用户希望将图像添加到他的个人资料中.该图像已存在于Web上,用户提供了直接URL.我不希望热链接图像,而是从我的域导入和提供服务.
Cia*_*lty 149
如果您的服务器上启用了PHP5并启用了HTTP流包装器,则将其复制到本地文件非常简单:
copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');
Run Code Online (Sandbox Code Playgroud)
这将处理所需的任何流水线等.如果您需要提供一些HTTP参数,则可以提供第三个"流上下文"参数.
daz*_*act 31
使用
$imageString = file_get_contents("http://example.com/image.jpg");
$save = file_put_contents('Image/saveto/image.jpg',$imageString);
Run Code Online (Sandbox Code Playgroud)
小智 10
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变量,不要将文件保存到磁盘.
| 归档时间: |
|
| 查看次数: |
133384 次 |
| 最近记录: |