6

Here's what I'm dealing with. One of our programs has a support form that users use to request support. What this form does is, it performs an HTTP POST request to a PHP script that's supposed to collect the info and forward it to the support e-mail address.

The POST request contains three text fields of the type Content-Type: text/plain which can be easily read in PHP using $_POST['fieldname']. Some of the content in this POST request, however, are files, of type Content-Type: application/octet-stream. Using $_POST doesn't seem to work with these files. How do I go about reading the contents of these files?

Thank you in advance.

djthoms
  • 3,026
  • 2
  • 31
  • 56
Jimmy
  • 61
  • 1
  • 1
  • 2
  • Please, oh, please, don't write "recieve". It's "receive". @Col. Shrapnel: he's probably meant `enctype` (the encoding information). – rhino Dec 06 '10 at 18:49

2 Answers2

11

You have to use the $_FILES superglobal:

$contents = file_get_contents($_FILES['myfile']['tmp_name']);

You can find more information in the manual. This will only work if you are using multipart/form-data encoding in your request.

You can otherwise read the raw POST data, then parse it yourself:

$rawPost = file_get_contents('php://input');
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • As I found out, PHP loads the entire file into memory when using the `php://input` method (and quickly runs out of memory with large files). Definitely switch to `multipart/form-data` and use $_FILES where possible. – Simon East Mar 27 '14 at 08:02
0

Those fall under the $_FILES superglobal. Read about it here: http://php.net/manual/en/features.file-upload.php

Basically, what you get is an array similar to POST, but with things like file size, temp name and the temporary location of the file which you can use to test and move the file to a permanent location.

Further general info from the PHP manual: http://php.net/manual/en/features.file-upload.php

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61