Welcome to the Cookbook

loading...

7.3.4 File Fields

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

To add a file upload field to a form, you must first make sure that the form enctype is set to "multipart/form-data", so start off with a create function such as the following.

echo $form->create('Document', array('enctype' => 'multipart/form-data') );
// OR
echo $form->create('Document', array('type' => 'file'));
  1. echo $form->create('Document', array('enctype' => 'multipart/form-data') );
  2. // OR
  3. echo $form->create('Document', array('type' => 'file'));

Next add either of the two lines to your form view file.

echo $form->input('Document.submittedfile', array('between'=>'<br />','type'=>'file'));

// or

echo $form->file('Document.submittedfile');
  1. echo $form->input('Document.submittedfile', array('between'=>'<br />','type'=>'file'));
  2. // or
  3. echo $form->file('Document.submittedfile');

Due to the limitations of HTML itself, it is not possible to put default values into input fields of type 'file'. Each time the form is displayed, the value inside will be empty.

Upon submission, file fields provide an expanded data array to the script receiving the form data.

For the example above, the values in the submitted data array would be organized as follows, if the CakePHP was installed on a Windows server. 'tmp_name' will have a different path in a Unix environment.


$this->data['Document']['submittedfile'] = array(
    'name' => conference_schedule.pdf
    'type' => application/pdf
    'tmp_name' => C:/WINDOWS/TEMP/php1EE.tmp
    'error' => 0
    'size' => 41737
);
  1. $this->data['Document']['submittedfile'] = array(
  2. 'name' => conference_schedule.pdf
  3. 'type' => application/pdf
  4. 'tmp_name' => C:/WINDOWS/TEMP/php1EE.tmp
  5. 'error' => 0
  6. 'size' => 41737
  7. );

This array is generated by PHP itself, so for more detail on the way PHP handles data passed via file fields read the PHP manual section on file uploads.

7.3.4.1 Validating Uploads

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

Below is an example validation method you could define in your model to validate whether a file has been successfully uploaded.

// Based on comment 8 from: http://bakery.cakephp.org/articles/view/improved-advance-validation-with-parameters

function isUploadedFile($params){
	$val = array_shift($params);
	if ((isset($val['error']) && $val['error'] == 0) ||
	(!empty( $val['tmp_name']) && $val['tmp_name'] != 'none')) {
		return is_uploaded_file($val['tmp_name']);
	}
	return false;
}
  1. // Based on comment 8 from: http://bakery.cakephp.org/articles/view/improved-advance-validation-with-parameters
  2. function isUploadedFile($params){
  3. $val = array_shift($params);
  4. if ((isset($val['error']) && $val['error'] == 0) ||
  5. (!empty( $val['tmp_name']) && $val['tmp_name'] != 'none')) {
  6. return is_uploaded_file($val['tmp_name']);
  7. }
  8. return false;
  9. }