zendframework文件上传功能实例代码

代码如下:

//实例化上传类$upload = new zend_file_transfer();//设置过滤器,大小限制为5m,格式为jpg,gif,png$upload->addvalidator(‘size’, false, 5 * 1024 * 1024);$upload->addvalidator(‘extension’, false, ‘jpg,gif,png’);if (!$upload->isvalid()) { print ‘文件大小或格式不符合’; exit();}

//获取上传的文件表单,可以有多项$fileinfo = $upload->getfileinfo();//获取后缀名,这里pic为上传表单file控件的name $ext = $this->getextension($fileinfo[‘pic’][‘name’]);//定义生成目录$dir = ‘./upload’ . date(‘/y/m/d/’);//文件重新命名do { $filename = date(‘his’) . rand(100000, 999999) . ‘.’ . $ext;} while (file_exists($dir . $filename));

//如果目录不存在则创建目录$this->makedir($dir);//将文件正式写入上传目录$upload->setdestination($dir);$upload->addfilter(‘rename’, array(‘target’ => $filename, ‘overwrite’ => true));if (!$upload->receive()) { print ‘上传图片失败’; exit();}

print $filename;

获取文件扩展名的方法:

代码如下:

/** * 获取文件扩展名 * * @param string $filename * @return string */public function getextension($filename) { if (!$filename) { return ”; } $exts = explode(“.”, $filename); $ext = end($exts); return $ext;}

创建目录的方法:

代码如下:

/** * 创建目录 * * @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.htmltecharticle 代码如下: //实例化上传类 $upload = new zend_file_transfer(); //设置过滤器,大小限制为5m,格式为jpg,gif,png $upload-addvalidator(‘size’, false,…

Posted in 未分类

发表评论