Home > Backend Development > PHP Tutorial > zend framework file upload function example code_PHP tutorial

zend framework file upload function example code_PHP tutorial

WBOY
Release: 2016-07-13 17:18:29
Original
788 people have browsed it

Copy code The code is as follows:

//Instantiate the upload class
$upload = new Zend_File_Transfer();
//Set the filter, the size limit is 5M, the format is 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 match';
exit();
}

//Get the uploaded file form, there can be multiple items
$fileInfo = $upload->getFileInfo();
//Get the suffix name, here pic is the name of the upload form file control   
$ext = $this->getExtension($fileInfo['pic']['name']);
//Define the generation directory
$dir = './upload' . date('/Y /m/d/');
//Rename the file
do {
$filename = date('His') . rand(100000, 999999) . '.' . $ext;
} while (file_exists($dir . $filename));

//Create the directory if it does not exist
$this->makeDir($dir);
//Formally write the file into the upload directory
$upload->setDestination($dir );
$upload->addFilter('Rename', array('target' => $filename, 'overwrite' => true));
if (!$upload->receive( )) {
print 'Failed to upload image';
exit();
}

print $filename;

How to get file extension:

Copy code The code is as follows:

/**
* Get file extension
*
* @param string $fileName
* @return string
*/
public function getExtension($fileName) {
if (!$fileName) {
return '';
}
$exts = explode(".", $fileName);
$ext = end($exts);
return $ext;
}

How to create a directory:

Copy code The code is as follows:

/**
* Create directory
*
* @param string $path
* @return boolean
*/
public function makeDir($path) {
if (DIRECTORY_SEPARATOR == "\") {//windows os
$path = iconv('utf-8', 'gbk', $path);
}
if (! $path) {
return false;
}
if (file_exists($path)) {
return true;
}
if (mkdir($path, 0777, true) ) {
return true;
}
return false;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621660.htmlTechArticleCopy the code The code is as follows: //Instantiate the upload class $upload = new Zend_File_Transfer(); //Set the filter , the size limit is 5M, the format is jpg, gif, png $upload-addValidator('Size', false,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template