PHP CURL 如何将一个网络图片上传至服务器

WBOY
Release: 2016-06-06 20:29:06
Original
1579 people have browsed it

一般利用curl将本地文件上传至服务器:
$file = realpath('gif/1.gif'); //要上传的文件
$fields['f'] = '@'.$file;

$ch = curl_init();
curlsetopt($ch, CURLOPTURL, $target_url);
curlsetopt($ch, CURLOPTPOST, 1);
curlsetopt($ch, CURLOPTPOSTFIELDS, $fields);
curlsetopt($ch, CURLOPTRETURNTRANSFER, 1);

$result = curl_exec($ch);
curl_close($ch);
echo $result;

如果是网络上的一个文件呢(例如图片),该如何上传

回复内容:

一般利用curl将本地文件上传至服务器:
$file = realpath('gif/1.gif'); //要上传的文件
$fields['f'] = '@'.$file;

$ch = curl_init();
curlsetopt($ch, CURLOPTURL, $target_url);
curlsetopt($ch, CURLOPTPOST, 1);
curlsetopt($ch, CURLOPTPOSTFIELDS, $fields);
curlsetopt($ch, CURLOPTRETURNTRANSFER, 1);

$result = curl_exec($ch);
curl_close($ch);
echo $result;

如果是网络上的一个文件呢(例如图片),该如何上传

可以先使用file_get_contents()去获取这个图片,并保存到本地,然后就跟你将本地上传一样了。

第一步

获取远程图片的 stream ;

第二步

使用 CURL 将远程图片的 stream 上传到你的服务器;

那么,如何将文件的 stream 上传到服务器呢?

参考这篇文章: CRUL上传图片全攻略

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!