2009-03-27 3 views
0

파일 업로드 중에 jpeg, png 및 gif 형식의 파일 만 허용되는지 확인하고 싶습니다. "형식의 파일 :"그래서 아래의 스크린 샷은 JPEG, PNG 및 GIF 제시해야합니다 :Symfony에서 Mime_type 확인 설정

http://lh5.ggpht.com/_SDci0Pf3tzU/ScynOZt_0qI/AAAAAAAAEo0/gMr_zxYofV4/s400/styleerror.png

을 나는 심포니 내 검사기에 대해 다음을했다 :

$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' 
    ) 
    ) 
    ); 

을 시도하지만, 너무 작동하지 않습니다.

public function executeIndex(sfWebRequest $request) { 
     $this->form = new UploadCaseForm (); 

if ($this->getRequest()->getMethod() == sfRequest::POST) { 
     var_dump($request->getParameter('UploadCase')); 


    } 

} 

편집 : 이 허용 대답 여기

<?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.')); } } ?> 

액션 코드입니다 :

나는 내 폼 클래스에 다음,하지만 불행히도 그것은 잘 작동하지 않습니다 시도
대답은 서버 측 유효성 검사가 진행되는 한. 클라이언트 측 유효성 검사 (예 : 서버가 작동하기 전에도 업로드 할 수있는 파일 유형 필터링)를 원하면 there is no reliable way to do it, because of browser's constraint 일 수 있습니다.

답변

3

API를 잘못 사용하는 것 같습니다. sfForm :: setValidator()의 서명은 here입니다.

image/jpeg 
image/pjpeg 
image/png 
image/x-png 
image/gif 

경우 :

$this->validatorSchema['my_file'] = 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.' 
)); 

은 'web_images'카테고리는 다음과 같은 MIME 유형이 포함됩니다 :

그러나, 여기에 전용 웹 이미지를 업로드 할 수있는 파일 검사기를 설정하는 간단한 방법입니다 수락 할 MIME 유형을 명시 적으로 정의하려면 'web_images'를 다음과 같은 유형의 배열로 바꿉니다.

'mime_types' => array(
    'image/jpeg', 
    'image/pjpeg', 
    'image/png', 
    'image/x-png', 
    'image/gif' 
) 
+0

고마워요.하지만 내 마임 유형은 이미지 만 포함하면 안됩니다. 또한 zip 파일 또는 워드 문서 파일을 포함해야합니다. – Graviton

+0

확인을 클릭하여 질문에서 이미지 만 원했던 것처럼 보일 것입니다. 사용자 지정 MIME 형식을 설정하는 방법에 대한 설명으로 내 대답을 편집했습니다. – Ree

+0

불행히도 그것은 작동하지 않는 것 같습니다; – Graviton

0

브라우저에서 업로드 파일을 제한하는 방법이 있습니다.
단점은 플래시를 사용하여 파일을 선택하고 플래시 플레이어가 설치된 사용자에게 지원을 제한한다는 것입니다. 이점은 플래시가 업로드 (parrallel 업로드, 진행, 오류, controle 파일 확장자 선택 등 허용)를 훨씬 더 제어 할 수 있다는 것입니다.

나는이에 사용하는 구성 요소는 swfupload

편집은이는 유사한 보안 leel과 더 나은 사용자 경험을 (오류 제공 업로드 된 파일에 대한 서버 측 제어를 수행로부터 면제되지 않지만, 함께 사용 장기 실행 업로드 시작 전에 발생할 수 있습니다.)

관련 문제