File upload

Contents...

System class waRequest offers convenient tools to manage files uploaded via the HTTP protocol (using web forms elements <input type="file">).

Uploading a single file

Suppose we have the following web form with one input element of type file:

<form action="..." method="post" enctype="multipart/form-data">
  ...
  <input type="file" name="photo">
  ...
</form>

In order to process the file uploaded through this form, use method waRequest::file() returning an instance of class waRequestFile, which will allow you to perform further operations on the uploaded file.

In fact, an instance of class waRequestFileIterator is returned, but in this case it acts as a transparent wrapper around the single instance of class waRequestFile in the file list. Read more about class waRequestFileIterator below in subsection "Uploading multiple files".
$file = waRequest::file('photo');

The fields of class waRequestFile are accessible only for reading; their meanings are completely identical to those of the $_FILES array items:

Methods of class waRequestFile:

try {
  $file->waImage()
       ->resize(...)
       ->crop(..)
       ->save(...);
} catch(Exception $e) {
    echo "The uploaded file is not a valid image file or some other error occurred: ".$e->getMessage();
    return;
}

Read more about class waImage in section "Image processing".

Uploading multiple files

Suppose you have a web form with several file uploading elements input of type file with equal names:

<form action="..." method="post" enctype="multipart/form-data">
  ...
  <input type="file" name="photo[]">
  <input type="file" name="photo[]">
  <input type="file" name="photo[]">
  ...
</form>

Method waRequest::file() returns an instance of class waRequestFileIterator:

$files = waRequest::file('photo');

Class waRequestFileIterator offers the same methods as waRequestFile. They work so as if they were applied to the first file in the list. Method $files->uploaded() determines whether at least one file has been uploaded.

Class waRequestFileIterator implements interface Iterator; it means that you can use a foreach cycle to access each uploaded file's properties. Each array item constitutes an instance of class waRequestFile, for which all the above mentioned methods are available; e.g.:

foreach($files as $file) {
    ...
}