0

I'm trying to send a .txt file via php curl post request to get its contents but no success.

The point is that if I make a var_dump($_FILES) as $result being $result the curl_exec($ch) response it shows me an empty array but if I try it with $_POST:

array(
    [uploaded_file] => 
        [name] => 'absolute/path/to/file.txt'
        [mime] => 'text/plain'
        [postname] => 'file.txt'
)

How can I pass that file as a $_FILES variable?

This is my curl send.php curl script:

$filedata = curl_file_create('../path/to/file.txt', 'text/plain', 'file.txt');

$array = array(
    'file' => $filedata
)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain_name.com/url/to/script.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = curl_exec($ch);
curl_close($ch);    

This is the receive.php script:

if(file_exists($_FILES['uploaded_file']['tmp_name']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
    $contents = file_get_contents($_FILES['uploaded_file']['tmp_name']);
    $data = explode('/',$contents);
}

but it's never entering the if statement...

molinet
  • 266
  • 3
  • 14

1 Answers1

0

You can't pass the file via cURL to make it appear in the $_FILES variable.

That is because the $_FILES variable is used for uploaded files using an HTML form submission.

For security reasons, the file must be encrypted first, and decrypted in the receiver page. I suggest you to use an encryption method that let you to choose the encryption's key, such as sha1.

You can find more informations here: https://stackoverflow.com/a/15200804/2342558

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • Then how can I send its contents to a remote server via curl? – molinet Jan 17 '18 at 15:53
  • That doesn't tell me how to get the file in the remote server script... – molinet Jan 17 '18 at 16:07
  • Inside the post you can find another link: http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/ here is explained how to do that. – user2342558 Jan 17 '18 at 16:11
  • I'm currently using php7 so `'@'` is deprecated. Should I try using that and setting `CURLOPT_SAFE_UPLOAD` to false? – molinet Jan 17 '18 at 16:14