I am trying to transfer the image from the remote server to the controller for further processing.

$url_image = 'http://vpoltave.info/uploads/ab/e977e56d31c874a1780528411755e2-bigimg.png'; $image = file_get_contents($url_image); $mime_type = getimagesize($url_image)['mime']; $file_name = basename($url_image); $boundary = '--------------------------' . md5(microtime(true)); $eol = '\r\n'; $url = SITE_URL . '/upload/post-photo'; $file_content = '--' . $boundary . $eol . 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . $eol . 'Content-Type: ' . $mime_type . $eol . $eol . //'Content-Transfer-Encoding: binary' . $eol . $eol . $image . $eol; $file_content .= '--' . $boundary . '--' . $eol; $response = file_get_contents($url, false, stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary=' . $boundary, 'content' => $file_content ] ])); var_dump($response); exit; 

At this point in the controller stub:

 var_dump($_FILES); exit; 

Request Headers:

 { "Content-Type":"multipart/form-data; boundary=--------------------------50fdc74116a157d67cdcd07aaf7dbf29", "User-Agent":"PHP (mysite.com), hosted by www.ukraine.com.ua", "Content-Length":"471222", "Connection":"close", "Geoip-Country-Code":"UA", "X-Real-Ip":"xx.xxx.xxx.xx", "Host":"mysite.com" } 

But in $ response, an empty array is always returned. Tell me what is missing ... Why is the $ _FILES array empty?

  • one
    I think it's worth curl to use. - Naumov
  • I considered this option, but they did not succeed there either. Probably missing something in the headers or encoding, although comparing with the file sent through the html form, the file was sent in this form. base64 also tried. This method, in my opinion, looks simpler than curl. Therefore, while continuing to dig in this direction. - ViZeR
  • Show how curl you try? - Naumov
  • Unfortunately, roll back the code until that moment does not work. I redid that code with this one. - ViZeR
  • In vain have to figure it out together. I'll find something - Naumov

1 answer 1

Sly mistake. Not immediately noticed.

Everything is fine with you, only in one trifle were mistaken - in quotes.

For \r\n , double quotes should be used, since it is in them that PHP recognizes control sequences :

 $eol = "\r\n"; 

But you can rewrite the code completely, using CURL . The above example has just been tested. I do not really like the creation of a temporary file, perhaps there is a way out without it. But it works as needed and in this form.

 $url_image = 'http://vpoltave.info/uploads/ab/e977e56d31c874a1780528411755e2-bigimg.png'; $url = 'http://192.168.1.3/file.php'; $ext = explode('.', $url_image); $ext = array_pop($ext); $tmpFileName = md5(time().$url_image).'.'.$ext; $content = file_get_contents($url_image); file_put_contents($tmpFileName, $content); $post = array('file' => '@'.realpath($tmpFileName)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $result = curl_exec($ch); curl_close($ch); unlink($tmpFileName); var_dump($result); 
  • There is a complex problem ... - Naumov
  • @Naumov come on, I don’t see any reason for him to rewrite the code, if you just correct the quotes. Unless, of course, he plans to upgrade, reuse and expand this code. Not beautiful? I agree. But it works. And, most often, only this is required. - Ivan Pshenitsyn
  • YES! Code earned. Thank you Such an idea came, but it seemed illogical and threw back this moment without even testing it)) - ViZeR
  • The fact is that it is better to do it right at once, and not to correct the castilla, unfortunately if you need to get the info file_get_contents would be better if you need to send something, it’s better to curl since it automatically puts you in accordance with the RFC standards. in general, do not reinvent the wheel. - Naumov
  • @Naumov for the most part, I agree with you. In my work, I adhere to the ideology of “doing it right immediately. But on the other hand, I follow the principle “why do extra work”. If the code works and works stably, this is the right code. He could be better, yes. And in short. But if the current version has no specific problems, it is valid. I want to add that all of the above refers rather to the situation when there is no ready-made version of the "correct" code and you need to work on it. In the current situation, when the ready code is provided, I see no reason to choose the "bicycle" option with file_get_contents . - Ivan Pshenitsyn