Почтовый файл PHP с файлом_get_contents

Мне нужно отправить файл с функцией file_get_contents PHP. Я использую код ниже, и я не получаю …

 $image['tmp_name'] = "/tmp/unnamed.png"; $image['file_image'] = base64_encode(file_get_contents("/tmp/unnamed.png")); $image['name'] = "unnamed.png"; $image['file_name'] = "unnamed.png"; $image['type'] = "image/png"; $image['submit'] = "UPLOAD"; $url = 'http://localhost/api/upload_image'; $postdata = http_build_query( array( 'file_name' => 'form variable1', 'file_image' => file_get_contents($image['tmp_name']) ) ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: multipart/form-data', 'content' => $postdata ) ); $context = stream_context_create($opts); var_export(file_get_contents($url, false, $context)); 

И на моем сервере в python у меня есть следующий код:

 class UploadImage(webapp.RequestHandler): def post(self): img = Images() img.image = self.request.get('file_image') img.name = self.request.get('file_name') or "no name" img.put() self.response.headers.add_header('content-type', 'application/json', charset='utf-8') self.response.out.write('{"status": "OK", "image": "'+img.key.urlsafe()+'"}') 

Мои изображения – это Google NDB Model с полем «ndb.BlobProperty ()».


Отредактировано: С этой работой CURL, но я не могу использовать на сервере производства ….

  $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST, 1 ); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($image) ); $exec = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); var_export($exec); 

ПОСТАНОВИЛИ:

  $image['file_image'] = base64_encode(file_get_contents($image['tmp_name'])); $image['file_name'] = "unnamed.png"; $postdata = http_build_query($image); $opts = array('http' => array( 'method' => 'POST', 'content' => $postdata ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $data); $info = $this->parse_http_response_header($http_response_header); var_export($result); 

Вы должны изменить заголовок на «Content-Type: application / x-www-form-urlencoded», и вам не нужно прибегать к завиту. См. Как отправлять данные на PHP с помощью file_get_contents? для примера.