I'm trying to upload files within a normal form with other text fields.
So far, the file gets uploaded to a temp folder but not to my destinationfolder, I always get this error "File 'upload' was illegally uploaded. This could be a possible attack".
I've checked the filename of the tempfile and that has the correct url in the correct folder.
What am I missing here.
$form = new Zend_Form();
$form->setAttrib('enctype', 'multipart/form-data');
$form->setMethod('post')
->addElement('file', 'pdf', array(
'size' => '40',
'label' => 'Select File',
'required' => true,
'validators' => array(
'Size' => array('min' => 20, 'max' => 1000000)
)
)
)
->addElement('submit', 'Save')
;
if ( $this->getRequest()->isPost() ) {
if ( $form->isValid($this->getRequest()->getParams()) ) {
$id = $form->getValue('name');
$upload = new Zend_File_Transfer_Adapter_Http();
$uploadDestination = APPLICATION_PATH . '/../public/uploads/'.$id;
if(!is_dir($uploadDestination)){
mkdir($uploadDestination, 0777, true);
}
$upload->setDestination($uploadDestination);
echo $upload->getFileName();
if($upload->receive('pdf'))
{
echo '<pre>';
print_r($form->getValues());
die();
}
else
{
$messages = $upload->getMessages();
echo implode("\n", $messages);
die();
}
$upload->receive('pdf'); is what's not working properly.
I know it's been a couple years, but here is the correct answer before you pull out your hair:
"Set if the file will be uploaded when getting the value. This defaults to false which will force receive() when calling getValues()."
think things may have improved in Zend Framework since this question was asked.
The code below shows a working example of a robust file validation, including customised error messages.
The key point is that the Zend_Form::isValid() method is all you need, you don't need to validate the file transfer separately
Your form definition, note that the file validators are added as if they were normal validators
your controller class
Try this:
I think you might have solved the problem... so i am just giving answer so that anyone who faces this error can find the solution via this valuable post... I faced many difficulties to solve this problem.... Hope it helps someone...
Here the problem is that isValid method is called twice.. once when you call
and second in the receive method.. so if you keep the below code outside $form->isValid method than your file will be uploaded..
but still you will get the error when you validate your other elements but your file will be uploaded...