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.


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

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

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&'.

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


You might also be interested in these tutorials too...

Wed, Nov 9, 2011

The Zend Framework Bootstrap made simple (Part 3)

Ok, we’ve established that with the Zend Framework, we need to do a bit more work than some of the other frameworks to get up to speed - but that’s not necessarily a bad thing - right?! But it can be a bit tedious and it’s something as professional developers, we want to automate away. So we’ve been addressing in this series how to do just that with a custom, extendable bootstrap class.

In the first part of the series we laid the foundation of our custom bootstrap class by creating a custom class directory structure, adding its namespace to the application ini and modifying the default application bootstrap so that it extends from it and had a look at the first component - caching.

Then, in the second part of the series, we built on the foundation laid in part one by creating plugin resources for the routing table, application navigation and the database connections - these being some of the most common tasks, usually, associated with a web-based application.

In this, the third and final part of the series, I’m going to finish up by looking at application placeholders and surprise, no not logging as originally promised, but pagination. As an added extra, we’re going to be using a key feature of Zend Application to make it a breeze.

Wed, Nov 2, 2011

The Zend Framework Bootstrap made simple (Part 2)

In the first part of the series, you’ll remember that we laid the foundation of our custom bootstrap class by creating a custom class directory structure, adding its namespace to the application ini and creating our custom bootstrap file that our application bootstrap will extend from.

After we did that, we put in the first but arguably the most important plugin resource – caching and stored it in the application registry. In this post we’re going to be building on that work and adding in three new plugin resources: routing, navigation and databases.

Thu, Oct 27, 2011

The Zend Framework Bootstrap Made Simple (Part 1)

When you’re creating a new project with the Zend Framework, unlike other frameworks, you need to do more legwork. This isn’t necessarily a bad thing, but it can sure slow you down when you’re trying to plough through a project.

I really enjoy using it, as it has a very well structured approach – and I like structure – it clearly lays out a file-system structure for modules, controllers, actions, forms, models and so on. It has good, but basic, tooling, allowing for modest project initialisation. But despite all this, it still requires a healthy investment on our part to get a proper foundation in place to use it productively.

In a recent project I encountered this situation and felt that I mustn’t be the only one to do so. As I plan to keep using Zend Framework I want to work around this situation and get as much productivity out of it as possible right from the get go. But how to do this?

Bootstrapping

Well the primary focus for me is bootstrapping. It provides the majority of the core services that every project needs, from routing, data source connections, authentication, authorisation, navigation, caching and so on. So it stands to reason that it’s a good place to start. So I want to cover what should go in to a good working bootstrap.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus