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'));
echo $form->create('Document', array('enctype' => 'multipart/form-data') );// ORecho $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');
echo $form->input('Document.submittedfile', array('between'=>'<br />','type'=>'file'));// orecho $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
);
$this->data['Document']['submittedfile'] = array('name' => conference_schedule.pdf'type' => application/pdf'tmp_name' => C:/WINDOWS/TEMP/php1EE.tmp'error' => 0'size' => 41737);
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;
}
// Based on comment 8 from: http://bakery.cakephp.org/articles/view/improved-advance-validation-with-parametersfunction 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;}


























