Integrating Upload class into Zend Framework 2 (ZF2)

See all posts Reply

Integrating Upload class into Zend Framework 2 (ZF2) new!
by Tom Herlihy, 10 years, 2 months ago
You can easily modify the Upload class to make use of it within the ZF2 MVC environment by following these steps:
(1) Rename the 'class.upload_0.32' folder 'Upload'.
(2) Within that folder, rename two php files: (A) rename 'upload.php' to 'upload_script.php'; so that you can (B) rename 'class.upload.php' to 'Upload.php'.
(3) Insert a new PHP file into the folder, with the name ' Module.php'. The contents of this short file appear at the bottom of this Forum entry.
(4) Following ZF2 conventions from here on, place the modified 'Upload' folder in your application's Vendor directory/folder.
(5) To tell your application to look for and load the 'Upload' class, add the following to the module list in the 'config/application.config.php' file found in every ZF2 application: 'Upload,'. Note the comma after the word.
(6) In the 'IndexController' or any other Controller file in which you wish to use the Upload class, add the following at the top of the file after the namespace line: 'use Upload/Upload;'.

That's it. You use the standard PHP scripting for Upload within your Controller actions. Following is the contents of the Module.php file:
namespace Upload;
class Module
{
  public function getAutoloaderConfig()
  {      
    return array(
      'Zend\Loader\StandardAutoloader' => array(
        'namespaces' => array(
          __NAMESPACE__ => __DIR__,
        ),
      ),
    );
  }
}
Reply
Re: Integrating Upload class into Zend Framework 2 (ZF2) new!
by Tom Herlihy, 10 years, 2 months ago
As the author of the original entry, I'd like to add one caution (that has been raised already in this Forum about a month ago but bears repeating) that has nothing to do with ZF2 but can prevent compilation of the Upload class, and that is that PHP 4.x and newer versions are strict about an aspect of preg_match() constructions, and refuse to compile when a hyphen in a line is not escaped (meaning preceded by a '\' character).

On about line 3014 of my copy of the Upload class, there are four cases where portions of two lines contain a '-w' that needs a '\' added before the hyphen. Here are the UNCORRECTED lines you will find that need the added '\' in four places:

// default to MIME from browser (or Flash)
if (!empty($mime_from_browser) && !$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime)) {
  $this->file_src_mime =$mime_from_browser;
  $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by browser
';
  if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
    $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
    $this->log .= '- MIME validated as ' . $this->file_src_mime . '
';
  } else {
    $this->file_src_mime = null;
  }
}
Reply
A flaw in Step (6) of the original email new!
by Tom Herlihy, 10 years, 2 months ago
Sorry all, but Step (6) showed a one-line addition to the IndexController file (or other file in which the Upload class will be used) that showed a forward slash (/) when it should have showed a backslash (\). So, the correct Step (6) is as follows:

(6) In the 'IndexController' or any other Controller file in which you wish to use the Upload class, add the following at the top of the file after the namespace line: 'use Upload\Upload;'.Reply