Trying to transfer an image from a 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' = & gt; [
'Method' = & gt; 'Post',
'Header' = & gt; 'Content-Type: MultiPart / Form-Data; boundary = '. $ boundary
'Content' = & gt; $ File_Content
]
]));
VAR_DUMP ($ Response); exit;
At the moment in the controller, the plug:
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 an empty array is always returned to $ response. Tell me what is missing … Why is an array $ _files is empty?
Answer 1, Authority 100%
Sunshine. Not immediately drew attention.
everything is fine with you, only in one little things made a mistake – in quotes.
For \ R \ N
you should use double quotes, since it is PHP that recognizes Control sequences :
$ Eol = "\ r \ n";
But you can rewrite the code completely, using CURL . The given example has just tested. I do not like creating a temporary file, perhaps there is a way out without it. But it works as necessary 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' = & gt; '@'. 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);