Zend Zend_File_Transfer_Adapter_Http renaming question
Asked Answered
B

6

5

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';
        }
    }
}
Boeschen answered 19/12, 2008 at 13:10 Comment(0)
O
9

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
Omnibus answered 28/10, 2009 at 23:25 Comment(0)
N
6

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();
Nonagon answered 18/2, 2009 at 9:49 Comment(0)
L
1

According to the documentation, you should not put the path in the destination

link text

Lentiginous answered 16/1, 2009 at 10:35 Comment(0)
M
1

there is a better and more secure way with zend framework ...

create helper class to retrieve file extension . .

class ImageUpload {

public function getExtension ($name)
{
    if($name){
    foreach ($name as $val){
        $fname=$val['name'];
      }
    $exts = split("[/\\.]", $fname) ;
    $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts; 
    }

}

}

in controller :

class ProfileController extends Zend_Controller_Action { function indexAction() { $this->view->title = "Profile"; $this->view->bodyCopy = "

Please fill out this form.

";
    $form = new ImgForm();

    if ($this->_request->isPost()) {
        $formData = $this->_request->getPost();
        if ($form->isValid($formData)) {
                $adapter = new Zend_File_Transfer_Adapter_Http();
                $adapter->setDestination('images/users/big');

                // getting extension

                $filename = $adapter->getFileInfo();
                $uhelper = new ImageUpload;  // cals for help to get file extension
                $extension = $uhelper->getExtension($filename); // got extension   


                 // success -- file handled

                 //rename
                 $auth = Zend_Auth::getInstance();
                 $identity = $auth->getIdentity();

                 $adapter->addFilter('Rename', array('target' => 'images/users/big/'.$identity->id.'.'.$extension,
                     'overwrite' => true));

                if (!$adapter->receive()) {
                 $form->addError($adapter->getMessages());

                }

        } else {
            $form->populate($formData);
        }
    }

    $this->view->form = $form;

}

when your form should be :

    parent::__construct($options);
    $this->setName('upload');
    $this->setAttrib('enctype', 'multipart/form-data');


    $file = new Zend_Form_Element_File('file');
    $file->setLabel('File')
          ->addValidator('Count', false, 1)     // ensure only 1 file
          ->addValidator('Size', false, 102400) // limit to 100K
          ->addValidator('Extension' ,false, 'jpg,png,gif') // only JPEG, PNG, and GIFs

          ->setRequired(true);          


    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setLabel('Upload');

    $this->addElements(array($file, $submit));

}

}

Have fun

Merrilee answered 24/3, 2009 at 16:7 Comment(0)
V
1
public function getExtension($name){
    $names= explode(".", $name);
    return $names[count($names)-1];
}
Vermeil answered 23/4, 2010 at 5:55 Comment(0)
C
0

I had to intercept the $_FILES and make the change before making the call to the adapter

if(isset($_FILES['file1'])){
  $ext = pathinfo($_FILES['file1']['name']);
  $_FILES['file1']['name'] = 'image_'. $userid .'.'.$ext['extension'];
}
$adapter = new Zend_File_Transfer_Adapter_Http();

Im sure there is a better way and I dont know why the filter doesn't work I have tried everything to get it to work. I had a deadline and so the above code went production LOL

Hope it helps someone

Eric

Caesarean answered 3/2, 2009 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.