Rename uploaded files with Zend Framework

Recently I was asked how to rename a file with the Zend Framework that used a Zend Form and Zend File element. They key requirement was that it should not be a hack or a kludged solution. So I thought I'd write a quick post to provide a simple example on how it was achieved.

So the first method that came to mind was a file filter. At first I thought that I may have to write my own, but then, on going through the available file filters that come with the standard Zend library, I found the rename filter - which did exactly what was needed. Ok, so let's skip to the chase and show the code, then we'll go through it.

The Code

[php] public function fileUploadAction() { $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(); $this->view->messages = array(&'File uploaded'); }

$this->view->form = $form; } [/php]

In the code above, we have a pretty standard, and simple, Zend Form setup. We've set the action and method of the form itself. After that we've created a file element to add to the form.

On the element, we've set the label and the required flag, so that required as part of the validation process. Nothing too difficult here.

Then we get to the core work - we add a filter on it. The filter used is the Zend Filter File Rename. In it, we've set only one parameter - target. Let's look at the available configuration options and I'll cover why just target is set.

Zend File Filter Rename Options

The file rename filter takes three arguments:

  • source - The file (or directory) to rename
  • target - The new file (or directory) name
  • overwrite - True/False indicating whether to overwrite an existing file of the same name or not

Given that the file's name been set by the name of the file element, in this case uploadfile there's no need to specify source. Also, in this simple example, there will be no existing file located in the upload directory, so there's no need to worry about overwriting it.

So we can skip this option as well. All that's needed to be done is to set target, which specifies what the file will be renamed to after validation has occurred - in this case &'config.ini&'. So now, when you run this and you look in your PHP temp directory, you'll see a new file called &'config.ini&'.

Nothing could be simpler.

Can this help you? Do you see problems with this approach? Tell us your thoughts in a comment or via twitter.

About Matthew

Matthew Setter Matthew Setter is a PHP & Zend Framework specialist. If you're in need of a custom software application, need to migrate an existing legacy application, or want to know your current application's GPA - get in touch.

Want To Be A Zend Framework Guru?

Drop your email in the box below, and get awesome tutorialsjust like this one — straight to your inbox, PLUS exclusive content only available by email.