Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用curl将url文件上载到远程服务器_Php_Curl - Fatal编程技术网

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

Php 使用curl将url文件上载到远程服务器,php,curl,Php,Curl,我已经做了3天了,但我似乎无法解决它,用户将输入url,系统将上传到远程服务器上。。这就是我到目前为止所做的 $POST_DATA = array( 'file' => '@'. 'http://images.visitcanberra.com.au/images/canberra_hero_image.jpg', 'extension' => 'txt', 'filename' => 'test', 'directory' => 'up

我已经做了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下载该文件。 将该图像上传到 并在成功上载到服务器后删除该文件。 这可能有助于您使用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为enable

我认为您需要将类型设置为multipart/formdata,否则您将得到无法携带文件的uriencoded。是否有错误?我怀疑上传文件名必须是本地文件,而不是URL。因此,您可能必须先将文件下载到本地文件,然后再上载。OP为什么要尝试此操作?一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客,以便他们可以找到这个问题并阅读你的答案。
$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;