2012-07-31 5 views
0

나는 파일 처리 목적으로 내 양식에 sfWidgetFormInputFileEditable을 사용합니다.Symfony 1.4, 업로드 된 파일 크기 및 이름 저장

/* File */ 
$this->widgetSchema['file_addr'] = new sfWidgetFormInputFileEditable(array(
    'label' => 'File', 
    'file_src' => sfConfig::get('sf_root_dir').'...', 
    'is_image' => false, 
    'edit_mode' => !$this->getObject()->isNew(), 
    'delete_label' => 'Delete?', 
    'template' => '...', 
    'with_delete' => true 
)); 
$this->validatorSchema['file_addr'] = new sfValidatorFile(array(
    'required' => $this->getObject()->isNew(), 
    'path' => sfConfig::get('sf_root_dir') .'/files' 
)); 
$this->validatorSchema['file_addr_delete'] = new deleteFileValidator(...); 
/* File: END */ 

file_addr 필드에 파일 이름을 생성 저장이 방법 : 다음은 코드입니다. 크기, 파일 이름 등의 다른 파일 데이터에 액세스하여 데이터베이스에도 저장하려고합니다. 어떻게해야합니까?

답변

2

추가 업로드 된 파일 데이터는 processValues 양식으로 제공됩니다. 따라서 다음과 같이 재정의합니다.

public function processValues($values) 
    { 
    /* File data */ 
    if ($values['file_addr'] instanceof sfValidatedFile) 
    { 
     $file = $values['file_addr']; 
     $this->getObject()->setFileSize($file->getSize()); 
     $this->getObject()->setFileType($file->getType()); 
     $this->getObject()->setFileHash(md5_file($file->getTempName())); 
    } 
    return parent::processValues($values); 
    } 
2

save() 메소드를 재정의 할 수 있습니다.

public function save($con = null) { 
    $values = $this->getValues(); 
    $fileAddr = $values['file_addr']; 

    // ... Do stuff with the values ... 

    return parent::save($con); 
} 

필자는 테스트하지 않았지만 그와 같은 것이 작동해야합니다.