0

I have the url to a file on a remote server. The url is something like this: http://www.example.com/file/. As you can see, I don't have the file extension, but when I execute this url directly in a browser, a zip file is download. What I'm trying to do is not to downloaded the file, but upload it to a different location (on my machine or another server). How can I do this?

I tried getting the content of the file with file_get_contents like this:

    $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);

And then trying to send the file with cURL like this:

    $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);

But when I do this I get the following error:

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

Any idea how to achieve this? Don't forget I'm using PHP version 5.3

Thanks in advance.

PS : upload.php is the file where I would actually perform the upload (by calling move_uploaded_file and using $_FILES, etc...)

user765368
  • 19,590
  • 27
  • 96
  • 167
  • 1
    `@` is supposed to be followed by the name of a file, not the contents of the file. – Barmar Jan 06 '17 at 20:18
  • Looks like you are trying to POST the file to localhost. Perhaps you could just write the file to your localhost file system using file_put_contents? – S. Imp Jan 06 '17 at 20:20
  • @S.Imp That looks like just an example, he says he also wants to be able to upload to a different server. He wrote: "on my machine or another server" – Barmar Jan 06 '17 at 20:22
  • @Barmar Just figured it was worth mentioning. In the event he wants to POST this file data to some other PHP script, I would imagine he needs to base64_encode its contents or something to avoid creating a broken request, but I'm not sure. If he gets 'request failed' it might be due to some weird quirk on the other end of things -- we need more info. – S. Imp Jan 06 '17 at 20:27
  • @S.Imp cURL does the encoding for you when you use `@filename` – Barmar Jan 06 '17 at 20:28
  • @Barmar so perhaps he should just write $file (the file's contents) to his local file system somewhere at some destination $filename and change the code to refer to @$filename instead? – S. Imp Jan 06 '17 at 20:30
  • 1
    @S.Imp Exactly. I closed this as a duplicate of a question that shows that solution. – Barmar Jan 06 '17 at 20:32
  • I'll give this a try and get back to let you guys know if it works. Thanks everyone. – user765368 Jan 06 '17 at 20:44

0 Answers0