ファイルのアップロード時に、jpeg、png、gif形式のファイルのみが許可されるようにしたいと思います。したがって、スクリーンショットの下の「ファイルタイプ:」は、jpeg、png、およびgifを表示する必要があります。
http://lh5.ggpht.com/_SDci0Pf3tzU/ScynOZt_0qI/AAAAAAAAEo0/gMr_zxYofV4/s400/styleerror.png
私はSymfonyのバリデーターに対して次のことを行いました:
$this->setValidator ( 'upload your filehere',
new sfValidatorFile ( array (
'required'=>true,
'mime_types' => array ('image/jpeg, image/png, image/gif' )
) ,
array(
'mime_types'=> 'only jpeg'
)
)
);
しかし、アップロードボタンをクリックしても、ファイルはそれに応じてフィルタリングされません。私は何を間違えましたか?
私もやってみます
$this->setValidator ( 'upload your filehere',
new sfValidatorFile ( array (
'required'=>true,
'mime_types' => 'image/jpeg, image/png, image/gif'
) ,
array(
'mime_types'=> 'only jpeg'
)
)
);
しかし、それも機能しません。
フォームクラスで次のことを試しましたが、残念ながらうまくいきません。
<?php
class UploadCaseForm extends sfForm {
public function configure() {
$this->setWidgets ( array ('Documents' => new sfWidgetFormInputFile ( ) ) );
$this->widgetSchema->setNameFormat ( 'UploadCase[%s]' );
$this->validatorSchema ['Documents'] = new sfValidatorFile (
array ('mime_types' => 'web_images' ),
array ('invalid' => 'Invalid file.',
'required' => 'Select a file to upload.',
'mime_types' => 'The file must be of JPEG, PNG or GIF format.' ) );
}
}
?>
アクションコードは次のとおりです。
public function executeIndex(sfWebRequest $request) {
$this->form = new UploadCaseForm ( );
if ($this->getRequest ()->getMethod () == sfRequest::POST) {
var_dump($request->getParameter('UploadCase'));
}
}
編集:サーバー側の検証が行われる限り、受け入れられた答えが答えです。クライアント側の検証、つまり、操作がサーバーに到達する前でもアップロードできるファイルの種類をフィルタリングする必要がある場合は、ブラウザーの制約により、信頼できる方法がない可能性があります。