Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Zend_File_Transfer_Adapter_Http renaming question

I've got a question about renaming a file after it's been uploaded in Zend. I don't know where to put the Rename Filter. Here's what I've got. I've tried moving things around, but I'm lost. Currently it does upload the file to my photos folder, but it doesn't rename it. Thanks for any help!

if($this->_request->isPost()) 
{
    $formData = $this->_request->getPost();

    if ($form->isValid($formData)) 
    {
        $adapter = new Zend_File_Transfer_Adapter_Http();
        $adapter->setDestination(WWW_ROOT . '/photos');

        $photo = $adapter->getFileInfo('Photo');

        $adapter->addFilter('Rename', array(
            $photo['Photo']['tmp_name'], 
            WWW_ROOT . '/photos/' . $this->memberId . '.jpg', 
            true
        )); 

        if ($adapter->receive()) 
        {
            echo 'renamed';
        }
    }
}
like image 668
Corey Avatar asked Dec 19 '08 13:12

Corey


2 Answers

Actually, there is an even easier way to do this. All you need to do is pass false as the second parameter for the getFileName method of the Zend_File_Transfer_Adapter_Http object. Then you can rename the file by appending a userID to it or parse the file name to get the extension out if you like as well.

// upload a file called myimage.jpg from the formfield named "image".

$uploaded_file = new Zend_File_Transfer_Adapter_Http();
$uploaded_file->setDestination('/your/path/');
    try {
        // upload the file
        $uploaded_file->receive();
    } catch (Zend_File_Transfer_Exception $e) {
        $e->getMessage();
    }
$file_name = $uploaded_file->getFileName('image', false);
// this outputs "myimage.jpg"

$file_path = $uploaded_file->getFileName('image');
// this outputs "/your/path/myimage.jpg"

// now use the above information to rename the file
like image 190
electromute Avatar answered Sep 17 '22 11:09

electromute


I managed to do that by setting a filter. Note that I did not set a destination path.

$adapter= new Zend_File_Transfer_Adapter_Http();
$adapter->addFilter('Rename',array('target' => WWW_ROOT . '/photos/' . $this->memberId . '.jpg'));

$adapter->receive();
like image 28
Marcel Avatar answered Sep 20 '22 11:09

Marcel