A
A
Artem Sorokin2016-02-27 16:39:11
PHP
Artem Sorokin, 2016-02-27 16:39:11

Why does cURL pass the path to the file to the server instead of the file?

Good afternoon!
I'm trying to set up a file transfer using PHP (cURL) from one server to another. On the sending server, the script looks like this:

<?php
  $cho = curl_init();
  $postdata['file'] = '@/var/www/html/file.jpg';		
  curl_setopt($cho, CURLOPT_URL, 'https://site.ru/upload.php');
  curl_setopt($cho, CURLOPT_POST, true);
  curl_setopt($cho, CURLOPT_POSTFIELDS, $postdata);
  curl_exec($cho); 
  curl_close($cho);
?>

On the receiving server, the following code:
<?php
print_r($_POST);
echo '<br>';
print_r($_FILES);
?>

The receiving script outputs:
Array ( [file] => @/var/www/html/file.jpg ) 
Array ( )

In other words, cURL does not transfer the file for some reason, but leaves it as a path (i.e. does not recognize what is required of it). There are no errors displayed anywhere.
If you try to send a file to upload.php using the html form, the file is normally processed and written to the $_FILES array.
I have already done this 100 times, but for some reason it doesn’t work - I’ve been trying to make it work since the very morning :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2016-02-27
@artyums

php.net/manual/en/function.curl-setopt.php
CURLOPT_SAFE_UPLOAD
TRUE to disable support for the @ prefix for uploading files in CURLOPT_POSTFIELDS, which means that values ​​starting with @ can be safely passed as fields. CURLFile may be used for uploads instead.
Added in PHP 5.5.0 with FALSE as the default value. PHP 5.6.0 changes the default value to TRUE.

curl_setopt($cho, CURLOPT_SAFE_UPLOAD, false); // required as of PHP 5.6.0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question