Reputation: 83264
In my Symfony application, I want to set zip file as one of the mime type during the file upload, the code to do this is below:
$this->validatorSchema ['Documents'] = new sfValidatorFile (
array ('mime_types' => array(
'application/zip',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
'application/x-zip',
'application/octet-stream',
'application/pdf') ),
array ('invalid' => 'Invalid file.',
'required' => 'Select a file to upload.',
'mime_types' => 'The file must be of JPEG, PNG , GIF, pdf and zip format.' ) );
However, when I did the uploads, I found that all of pdf
, png
, gif
etc can be uploaded. The only mime type that cannot be uploaded, are files that end with .zip
.
What's going wrong here?
I asked a related, but different question here.
Note: I am using Symfony 1.2.7
Edit: I did some further testing with different browsers. Firefox 3 works because of 'application/octet-stream'', whereas IE works because of 'application/x-zip-compressed', Google Chrome doesn't work at all.
Upvotes: 0
Views: 3761
Reputation: 1
There is a solution :
create your "sfValidatorFileZip.class.php" in your "/lib/Validator/". This class extend from the commun "sfValidatorFile" and it contain the "configure" methode as listed below :
class sfValidatorFileZip extends sfValidatorFile{
protected function configure($options = array(), $messages = array()){
if (!ini_get('file_uploads'))
{
throw new LogicException(sprintf('Unable to use a file validator as "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
}
$this->addOption('max_size');
$this->addOption('mime_types');
$this->addOption('mime_type_guessers', array(
array($this, 'guessFromFileinfo'),
array($this, 'guessFromMimeContentType'),
array($this, 'guessFromFileBinary'),
));
$this->addOption('mime_categories', array(
'web_images' => array(
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
),
'zip_file' => array(
'application/zip'
)
));
$this->addOption('validated_file_class', 'sfValidatedFile');
$this->addOption('path', null);
$this->addMessage('max_size', 'File is too large (maximum is %max_size% bytes).');
$this->addMessage('mime_types', 'Invalid mime type (%mime_type%).');
$this->addMessage('partial', 'The uploaded file was only partially uploaded.');
$this->addMessage('no_tmp_dir', 'Missing a temporary folder.');
$this->addMessage('cant_write', 'Failed to write file to disk.');
$this->addMessage('extension', 'File upload stopped by extension.');
}
}
in your form, the widget will be an instance from your custom "sfValidatorFileZip" an you have to precise that 'mime_types' => 'zip_file'
. exemple :
$this->setValidator('filename', new sfValidatorFileZip(array(
'required' => false,
'mime_types' => 'zip_file',
'path' => sfConfig::get('sf_upload_dir') . '/files/',),
array('required' => 'filename is required.', 'mime_types' => 'only ZIP is accepted')));
Upvotes: 0
Reputation: 510
this is a problem with mime type detection. the sfValidatorFile can use user supplied function to determine mime type. so you can write your own function to detect zip files from uploaded file if mime type that is determined by validator doesn't do the job right.
link to documentation, look at the end of page that explains file uploads.
Upvotes: 0
Reputation: 2837
Do an echo or error_log to see what $uploaded_file->getMime() [or whatever the right method call is] returns for your zip files.
If the string you get is one of the strings you pass to the array, there might be a bug with sfValidatorFile (I've never used it) and you might want to try using a yml validator.
Upvotes: 0