PHP:如何重命名使用Zend_Form_Element_File上传的文件?

And*_*rew 28 php zend-framework file-upload zend-file

形成:

//excerpt
$file = new Zend_Form_Element_File('file');
$file->setLabel('File to upload:')
    ->setRequired(true)
    ->addValidator('NotEmpty')
    ->addValidator('Count', false, 1)
    ->setDestination(APPLICATION_UPLOADS_DIR);
$this->addElement($file);
Run Code Online (Sandbox Code Playgroud)

控制器:

//excerpt
if ($form->isValid($request->getPost()) {
    $newFilename = 'foobar.txt';
    //how should I rename the file?
    //When should I rename the file? Before or after receiving?
    try {
        $form->file->receive();
        echo 'filename: '. $form->file->getFileName();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 当我调用$form->file->getFileName()它时,返回完整路径,而不仅仅是文件名.如何仅输出文件名?

    //Answer: First, get an array of the parts of the filename:
    $pathparts = pathinfo($form->file->getFileName());
    //then get the part that you want to use
    $originalFilename = $pathparts['basename'];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如何将文件名重命名为我想要的名称?这可以Rename用过滤器完成吗?我已经在表单中设置了目标,所以我想要做的就是更改文件名.也许我不应该在表单中设置目的地?或者这可能无法通过过滤器完成.也许我应该用PHP函数做这个?我该怎么办?

    //Answer: Use the rename filter:
    $form->file->addFilter('Rename', 'new-file-name-goes-here.txt');
    
    Run Code Online (Sandbox Code Playgroud)

最终解决方案

这就是我最终做的事情:

public function foobarAction()
{
    //...etc...

    if (!$form->isValid($request->getPost())) {
        $this->view->form = $form;
        return;
    }

    //the following will rename the file (I'm setting the upload dir in the form)
    $originalFilename = pathinfo($form->file->getFileName());
    $newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension'];
    $form->file->addFilter('Rename', $newFilename);

    try {
        $form->file->receive();
        //upload complete!
        $file = new Default_Model_File();
        $file->setDisplayFilename($originalFilename['basename'])
            ->setActualFilename($newFilename)
            ->setMimeType($form->file->getMimeType())
            ->setDescription($form->description->getValue());
        $file->save();
    } catch (Exception $e) {
        //error: file couldn't be received, or saved (one of the two)
    }
}
Run Code Online (Sandbox Code Playgroud)

Pas*_*TIN 6

要回答问题1,要从完整路径获取文件名,您可以使用basename,或pathinfo.

例如(从doc复制粘贴):

$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"
Run Code Online (Sandbox Code Playgroud)

要么 :

$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
Run Code Online (Sandbox Code Playgroud)


要重命名/移动文件,我想rename即使它不是"Zend Framework解决方案"也可以做到这一点.

如果文件尚未被ZF移动并且仍在临时目录中,则应使用move_uploaded_file- 但正如您所使用的那样setDestination,我认为该文件不再位于系统的临时目录中.


Mat*_*ter 5

伙计们,这是一个简单的表单,它在上传文件后对文件使用重命名过滤器.还有更多的选择,是的,你需要考虑现有的文件,但为了让你开始,你走了.

当文件通过下面的表单上传时,它将被重命名为' 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();
}
Run Code Online (Sandbox Code Playgroud)