Home > Article > Backend Development > zend framework file upload function example code_PHP tutorial
//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:
How to create a directory: