PHP:如何重命名用Zend_Form_Element_File上传的文件? [英] PHP: How to rename a file uploaded with Zend_Form_Element_File?

查看:134
本文介绍了PHP:如何重命名用Zend_Form_Element_File上传的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格:



  //摘录
$ 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);



控制器:



  //摘录
if($ form-> isValid($ request-> getPost()){
$ newFilename ='foobar.txt';
//我应该如何重命名文件?
//什么时候应该重命名文件?在接收之前或之后?
尝试{
$ form-> file-> receive();
echo'filename:'。$ form-> file-> getFileName();
}
}



问题:




  1. 当我调用 $ form-> ; file-> getFileName()它会返回完整的路径,而不仅仅是文件名,我怎样才能输出文件的名字?




    $ pathparts = pathinfo($ form-> file-> getFileName())返回一个数组;
    //然后得到你想使用的部分
    $ originalFilename = $ pathparts ['basename'];


  2. 如何将文件名重命名为wan t?可以用 Rename 过滤器来完成吗?我已经在表单中设置目标,所以我想要做的就是改变文件名。也许我不应该在表单中设置目的地?或者,也许这不能用一个过滤器来完成。也许我应该用PHP函数做这个?我应该怎么办?

      //回答:使用重命名过滤器:
    $ form-> file-> ; addFilter('Rename','new-file-name-goes-here.txt');




最终解决方案:



这就是我最终做的:

pre $ public function foobarAction()
{
// ...等等...

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


//下面将重命名文件(我在表单中设置上传目录)
$ originalFilename = pathinfo($ form-> file-> ;的getFileName());
$ newFilename ='file-'。 uniqid()。 ''。 $ originalFilename [延伸];
$ form-> file-> addFilter('Rename',$ newFilename);

尝试{
$ form-> file-> receive();
//上传完成!
$ file = new Default_Model_File();
$ file-> setDisplayFilename($ originalFilename ['basename'])
- > setActualFilename($ newFilename)
- > setMimeType($ form-> file-> getMimeType ())
- > setDescription($ form-> description-> getValue());
$ file-> save();
} catch(Exception $ e){
//错误:无法接收或保存文件(两者之一)
}
}


解决方案

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



em>(从文档复制粘贴):

  $ path =/ home / httpd / html /的index.php; 
$ file = basename($ path); // $ file设置为index.php

或者:

  $ path_parts = pathinfo('/ www / htdocs / index.html'); 
echo $ path_parts ['dirname'],\\\
;
echo $ path_parts ['basename'],\\\
;
echo $ path_parts ['extension'],\\\
;
echo $ path_parts ['filename'],\\\
; //自PHP 5.2.0开始





要重命名/移动文件,我想 rename 可以做到这一点,即使是相当的而不是Zend Framework解决方案。
$ b 如果文件没有被ZF移动,仍然在临时目录中,则应该使用 move_uploaded_file - 但是您正在使用 setDestination ,我想这个文件不在sytem的临时目录中。


Form:

//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);

Controller:

//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();
    }
}

Questions:

  1. When I call $form->file->getFileName() it returns the full path, not just the file name. How can I output just the name of the file?

    //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'];
    

  2. How can I rename the filename to something I want? Can this be done with the Rename filter? I'm already setting the destination in the form, so all I want to do is change the filename. Maybe I shouldn't be setting the destination in the form? Or maybe this can't be done with a filter. Maybe I should be doing this with a PHP function? What should I do?

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

Final Solution:

This is what I ended up doing:

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)
    }
}

解决方案

To answer question 1, to get a filename from a full path, you can use basename, or pathinfo.

For example (copy-paste from the doc) :

$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"

Or :

$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


To rename / move the file, I suppose rename would do the trick, even if it's quite not "Zend Framework solution".

If the file has not been moved by ZF and is still in the temporary directory, you should use move_uploaded_file -- but as you are using setDestination, I suppose the file is no longer in the sytem's temporary directory.

这篇关于PHP:如何重命名用Zend_Form_Element_File上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆