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;
}
http://www.bkjia.com/PHPjc/621660.htmlwww.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,...