使用 curl 上传 url 文件到远程服务器
Uploading url file to remote server using curl
我已经处理了 3 天了,但我似乎无法解决它,用户将输入 url,系统会将其上传到远程服务器..这就是我到目前为止已经完成
$POST_DATA = array(
'file' => '@'. 'http://images.visitcanberra.com.au/images/canberra_hero_image.jpg',
'extension' => 'txt',
'filename' => 'test',
'directory' => 'uploads/'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://1234.4321.67.11/upload.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
那就这样吧
- 首先从用户提供的 url 下载该文件。
- 在 http://1234.4321.67.11/upload.php
上上传该图片
- 并在成功上传到服务器后删除该文件。
这可能有助于您使用 cURL 在服务器上上传图片:
这可能会帮助您了解如何从 url 下载图像:
使用下面的函数使用curl上传远程图片。通过传递图像 url 和保存图像的文件夹路径来调用它。
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
并确保在 php.ini allow_url_fopen 中启用
$file_name_with_full_path = realpath('./upload/example.png');
$curl_handle = curl_init("https://example.com");
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile($file_name_with_full_path, 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$returned_data = curl_exec($curl_handle);
curl_close ($curl_handle);
echo $returned_data;
我已经处理了 3 天了,但我似乎无法解决它,用户将输入 url,系统会将其上传到远程服务器..这就是我到目前为止已经完成
$POST_DATA = array(
'file' => '@'. 'http://images.visitcanberra.com.au/images/canberra_hero_image.jpg',
'extension' => 'txt',
'filename' => 'test',
'directory' => 'uploads/'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://1234.4321.67.11/upload.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
$response = curl_exec($curl);
curl_close ($curl);
那就这样吧
- 首先从用户提供的 url 下载该文件。
- 在 http://1234.4321.67.11/upload.php 上上传该图片
- 并在成功上传到服务器后删除该文件。
这可能有助于您使用 cURL 在服务器上上传图片:
这可能会帮助您了解如何从 url 下载图像:
使用下面的函数使用curl上传远程图片。通过传递图像 url 和保存图像的文件夹路径来调用它。
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
并确保在 php.ini allow_url_fopen 中启用
$file_name_with_full_path = realpath('./upload/example.png');
$curl_handle = curl_init("https://example.com");
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile($file_name_with_full_path, 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$returned_data = curl_exec($curl_handle);
curl_close ($curl_handle);
echo $returned_data;