PHP: How to rename a file uploaded with Zend_Form_Element_File?

前端 未结 7 700
借酒劲吻你
借酒劲吻你 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:15

    You can use Rename filter. If you want to rename your filename during uploading maybe it helps you.

    First we need a function rename or remove unwanted characters from your filename for example use this.

    public function simple_fileformat($str)
    {
        $str = preg_replace("{[^A-Za-z0-9_]\.}", "", $str);
        $str = str_replace(" ", "_", $str);
        return $str;
    }
    

    After that you can use the above function for renaming.

    $filename = new Zend_Form_Element_File("filename");
    $filename->setLabel("filename");
    $filename->setRequired(true);
    $filename->setDestination($doc_path);
    $filename->addFilter("rename", $doc_path . DIRECTORY_SEPARATOR . $this->simple_fileformat(basename($filename->getFileName())));
    

    That is easy. Is not it?

提交回复
热议问题