webCOMAND

request::file()

Retrieve the contents of a file uploaded through an HTTP (or HTTPS) POST.

Note that the HTML form must have enctype="multipart/form-data" or other file-compatible encryption type.  See example below.

Prototype

string file(string $key = NULL, array $options = [])

Parameters

  • key - Name of the file input to retrieve the uploaded file contents for.  If NULL or no key is provided, an associative array of all files will be returned with the following key/value pairs.
    • name - The filename sent with the file data.
    • type - The content type send with the file data.
    • tmp_name - The temporary file on the server that contains the file data.
    • error - An error code if there was an error retrieving the data.  0 = no error.
    • size - The size, in bytes, of the file data.
  • options - An associative array of zero or more of the following key/value pairs.
    • default - The default value to return if the key is not found.

Return

If the file input exists and a file was uploaded, the file contents is returned as a string, including an empty string.  Otherwise, the default value specified in options is returned, or FALSE if no default specified.

Example

The following HTML form will POST a file with the key "upload".

<html>
<body>
    <h1>File Upload</h1>
    <form method="POST" enctype="multipart/form-data">
        <input name="upload" type="file" />
        <input type="submit" />
    </form>
</body>
</html>

The following PHP code will retrieve the posted file.

// retrieve the value of the "form" GET parameter
// and "search" POST parameter
$request = new \io_comand_web\request();
$filename = $request->filename('upload');
$filedata = $request->file('upload');