zend Framework File Upload function instance code, PHP version 5.3.8,zend framework version 1.12, look at the following code, there are comments
The code is as follows://instantiation of the upload class $upload = new Zend_file_transfer (); Set the filter to a size limit of 5 m, formatted as Jpg,gif,png $upload->addvalidator (' size ', FALSE, 5 * 1024 * 1024); $upload->addvalidator (' Extension ', false, ' jpg,gif,png '); if (! $upload->isvalid ()) { print ' file size or format does not conform to '; exit ();} //Get uploaded file form with multiple items $fileInfo = $upload->getfileinfo (); Gets the suffix name, where pic is the name of the upload form file control $ext = $this->getextension ($fileInfo [' pic '] [' name ']); Define the build directory $dir = './upload '. Date ('/y/m/d/'); File renames do { $filename = date (' his '). Rand (100000, 999999). '.' . $ext; while (File_exists ($dir. $filename)); //If the directory does not exist create the directory $this->makedir ($dir); The file is formally written to the upload directory $upload->setdestination ($dir); $upload->addfilter (' Rename ', array (' Target ' => $filename, ' overwrite ' => true)); if (! $upload->receive ()) { print ' upload image failed '; exit ();} print $filename; Get file extension method: Code as follows:/** * Get the file name extension * * @param string $fileName * @return string */public function getextension ($file Name { if (! $fileName) { return '; } $exts = Explo De (".", $fileName); $ext = end ($exts); return $ext; } How to create a directory: code as follows:/** * Create directory * * @param string $path * @return Boo Lean */Public Function MakeDir ($path) { if (Directory_separator = = "") {//windows os &N Bsp $path = iconv (' utf-8 ', ' GBK ', $path); } if (! $path) { return false; } if (fi Le_exists ($path)) { return true; } if (mkdir ($path, 0777, True)) { return true; } return false; }