简体   繁体   English

使用curl将url文件上传到远程服务器

[英]Uploading url file to remote server using curl

I've been on this for 3 days now but I can't seem to resolve it, the user will input the url and the system will upload it on remote server.. this is what I've done so far 我已经使用了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);

Well do it in this way. 用这种方式做得好。

  1. First download that file from url provided by user. 首先从用户提供的网址下载该文件。
  2. Upload that image on http://1234.4321.67.11/upload.php 将该图片上传到http://1234.4321.67.11/upload.php
  3. And delete that file after successfully uploaded on server. 成功上传到服务器后,删除该文件。

This might help you for uploading image on server using cURL : https://stackoverflow.com/a/15177543/1817160 这可能会帮助您使用cURL在服务器上上传图像: https : //stackoverflow.com/a/15177543/1817160

And this might help you in understanding how to download image from url : Copy Image from Remote Server Over HTTP 这可能有助于您了解如何从url下载图像: 通过HTTP从远程服务器复制图像

Use the following function to upload the remote image using curl. 使用以下功能通过curl上传远程图像。 Call this by passing image url and path to folder to save the image. 通过将图像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);
    }

and ensure that in php.ini allow_url_fopen is enable 并确保在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM