zend framework文件上传功能实例代码
时间:2024-05-05 09:17:17
//实例化上传类
$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;
}
标签:zend,framework,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php字符串过滤strip_tags()函数用法实例分析
2023-09-04 14:05:00
python产生模拟数据faker库的使用详解
2022-05-25 22:43:35
python scrapy框架中Request对象和Response对象的介绍
2021-04-02 07:29:59
![](https://img.aspxhome.com/file/2023/6/95326_0s.png)
pymongo insert_many 批量插入的实例
2023-05-30 04:27:43
教你用Python为二年级的学生批量生成数学题
2023-12-21 13:15:42
![](https://img.aspxhome.com/file/2023/8/128348_0s.png)
SQL Server 监控磁盘IO错误,msdb.dbo.suspect_pages
2024-01-22 09:26:44
Mysql中如何删除某个字段的最后四个字符
2024-01-28 22:44:07
浅析MySQL - MVCC
2024-01-24 14:29:22
zabbix通过percona插件监控mysql的方法
2024-01-23 04:51:22
![](https://img.aspxhome.com/file/2023/5/93895_0s.png)
非常不错的[JS]Cookie精通之路第1/2页
2023-09-04 04:04:34
Python中获取网页状态码的两个方法
2023-08-27 22:47:21
在js中调用asp页面的方法
2007-08-21 20:30:00
Python3 pandas 操作列表实例详解
2021-11-30 14:24:12
Python中的is和==比较两个对象的两种方法
2021-09-15 21:23:14
Iinternet Explorer浏览器简介(IE)
2009-02-05 20:59:00
Python3 实现随机生成一组不重复数并按行写入文件
2021-11-25 18:05:35
基于python实现上传文件到OSS代码实例
2022-12-14 16:45:17
vue2.0 自定义 饼状图 (Echarts)组件的方法
2024-04-27 15:51:07
![](https://img.aspxhome.com/file/2023/8/133098_0s.jpg)
Python 操作 MySQL数据库
2024-01-17 17:46:17
在pycharm中debug 实时查看数据操作(交互式)
2023-01-19 08:31:11
![](https://img.aspxhome.com/file/2023/4/66324_0s.jpg)