上載檔案,並修改檔案名稱的方式

方法1:

class StudentForm extends BaseStudentForm
{
  public function configure()
  {
    $this->widgetSchema['photo'] = new sfWidgetFormInputFile();
    $this->validatorSchema['photo'] = new sfValidatorFile();
  }
}

然後在action中修改檔案名稱

public function executeCreateStudent(sfWebRequest $request)
{
  $this->form = new StudentForm();

  if ($this->form->bindAndSave($request->getParameter('student'), $request->getFiles('student')))
  {
    $photo = $this->form->getValue('photo');
    $photo->save(sfConfig::get('sf_web_dir').'/uploads/students/'.$photo->getOriginalName());
  }
}

方法2:

class StudentForm extends BaseStudentForm
{
  public function configure()
  {
    $this->widgetSchema['photo'] = new sfWidgetFormInputFile();
    $this->validatorSchema['photo'] = new sfValidatorFile(array(
      'path' => sfConfig::get('sf_web_dir').'/uploads/students',
    ));
  }
}

然後在class中加入以下method,注意,method name Photo是field的名稱

class Student extends BaseStudent
{
  public function generatePhotoFilename(sfValidatedFile $file)
  { 
     return $this->getId().$file->getOriginalExtension();
    //return $file->getOriginalName();
  }
}

沒有留言: