PHP: How to rename a file uploaded with Zend_Form_Element_File?

前端 未结 7 725
借酒劲吻你
借酒劲吻你 2021-02-01 16:25

Form:

//excerpt
$file = new Zend_Form_Element_File(\'file\');
$file->setLabel(\'File to upload:\')
    ->setRequired(true)
    ->addValidator(\'NotEmp         


        
7条回答
  •  甜味超标
    2021-02-01 17:10

    Folks, here's a simple example of a form that uses the rename filter on a file after it's been uploaded. There are many more options and yes, you'd need to take in to consideration existing files, but to get you started, here you go.

    When the file's uploaded through the form below, it will be renamed to 'config.ini'.

    $form = new Zend_Form;
    $form->setAction('/default/index/file-upload')
         ->setMethod('post');
    
    $uploadFile = new Zend_Form_Element_File('uploadfile');
    $uploadFile->addFilter(new Zend_Filter_File_Rename(
                  array('target' => 'config.ini'))
               )
               ->setRequired(true)
               ->setLabel('Upload file:');
    
    $form->addElement($uploadFile);
    $form->addElement(new Zend_Form_Element_Submit('submit'));
    
    if ($form->isValid($_POST)) {
        $values = $form->getValues();
    }
    

提交回复
热议问题