Zend Framework File Upload function Instance code

Source: Internet
Author: User
Tags create directory file size file upload zend zend framework

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.