使用file_get_contents或cURL PHP 5.3上传远程zip文件

时间:2017-01-06 20:16:33

标签: php curl file-upload file-get-contents php-5.3

我有一个远程服务器上的文件的URL。网址如下: http://www.example.com/file/ 。如您所见,我没有文件扩展名,但是当我直接在浏览器中执行此URL时,会下载一个zip文件。我要做的不是下载文件,而是将其上传到其他位置(在我的机器或其他服务器上)。我怎么能这样做?

我尝试使用file_get_contents获取文件的内容,如下所示:

    $opts = array(
        'http'=>array(
            'method'=>"POST",
            'header'=>array('X-Authentication: '.$_POST['access_token'], 'Content-Length: '.$_POST['file_size'])
        )
    );

    $context = stream_context_create($opts);

    $file = file_get_contents('http://www.example.com/file/', false, $context);

然后尝试使用cURL发送文件:

    $post = array('extra_info' => $_POST['file_size'],'file_contents'=>'@'.$file);

    $curl2 = curl_init();

    curl_setopt_array($curl2, array(
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'https://localhost/manage_files/upload.php?action=do_upload',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $post
    ));

    curl_exec($curl2);

但是当我这样做时,我收到以下错误:

failed to open stream: HTTP request failed!
Error: "couldn't open file "" " - Code: 26

知道怎么做到这一点?别忘了我使用的是PHP 5.3版本

提前致谢。

PS :upload.php是我实际执行上传的文件(通过调用move_uploaded_file并使用$ _FILES等...)

0 个答案:

没有答案
相关问题