March 1, 2021
Hot Topics:

Managing File Uploads with the Zend Framework

  • By Jason Gilmore
  • Send Email »
  • More Articles »

Validating Your File Uploads

At the most basic level, you'll probably want to examine one or several key characteristics of the file being uploaded, including the file's size or MIME type. Several methods are available for retrieving these characteristics. For instance, to retrieve the file's MIME type, call getMimeType() after receiving the file:

  if ($upload->receive()) {
    echo "The file type is {$upload->getMimeType()}"; 

For instance, if you uploaded a PDF to the server and called the getMimeType method, it would return application/x-pdf. This can be quite useful when the user should be restricted to uploading files of a certain type. Here's an example:

  if ($upload->getMimeType() == "application/x-pdf") {
    echo "File received";
  } else {
    echo "Please upload a PDF";

Of course, examining the file size or type is only a small part of the validation process. To facilitate the many verifications you'll likely want to carry out before accepting an uploaded file, the Zend_File_Transfer component offers 18 validation methods. You can view a complete list of these validators by navigating to the appropriate section of the Zend Framework documentation. One of my favorite available validators is ImageSize, which will examine an uploaded image's dimensions, ensuring they fall within a preset minimum and maximum range. This can be very useful for situations in which you wanted to for instance give usrs the opportunity to upload an avatar thumbnail, but want to keep the thumbnails uniform. This example will ensure all uploaded images are of type PNG, and further, conform to a size of 160 by 160 pixels:

  if ($this->getRequest()->isPost()) {
    $upload = new Zend_File_Transfer_Adapter_Http();
    $upload->addValidator('MimeType', false, 'image/png');
    $upload->addValidator('ImageSize', false,
      array('minwidth' => 160,
        'maxwidth' => 160,
        'minheight' => 160,
        'maxheight' => 160)
    if ($upload->isValid()) {
      echo "File received";
    } else {
      echo "Please upload a file of type PNG and dimensions 160x160 pixels.";

Where to From Here?

With user-driven content of all formats now a crucial part of the Web, your project's success may depend upon just how efficiently that data can be transferred from the user to your Web server. The Zend Framework's Zend_File_Transfer component goes a long way towards reducing the amount of complexity and tedium involved in offering such features to your users!

About the Author

Jason Gilmore is founder of EasyPHPWebsites.com, and author of the popular book, "Easy PHP Websites with the Zend Framework". Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industry’s most respected publishing programs. Over the years he's authored several other books, including the best-selling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional.

Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer’s conference, and was a member of the 2008 MySQL Conference speaker selection board.

Jason has published more than 100 tutorials and articles within prominent publications such as Developer.com, Linux Magazine, and TechTarget.

Page 2 of 2

This article was originally published on November 6, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date