Zend Framework之Zend_File玩上传

Zend Framework之Zend_File玩上传

1.IndexController.php中加上下面的代码:
function uploadAction() {
$adapter = new Zend_File_Transfer_Adapter_Http ( );
$adapter->setDestination ( ‘d:\user’ );//设置上传路径
$adapter->addValidator ( ‘Extension’, false, ‘jpg,png,gif’ )//文件格式限制
->addValidator( ‘Size’, false, 1048576*2 )//设置上传文件的最大限制大小1048576*2为2M
//->addValidator(‘IsImage’, false, array(‘application/gif’))//检查上传的文件是不是gif
//->addFilter(‘Rename’, ‘D:\uploads’,’17.jpg’)//当上传17.jpg时,将被上传到D:\uploads中, d:\user中没有17.jpg
//->addValidator(‘Size’, false, array(‘min’ => 1048576*1, ‘max’ => 1048576*2))//设置上传文件的大小在1-2M之间
//->addValidator(‘ImageSize’, false,array(‘minwidth’ => 40,’maxwidth’ => 80,’minheight’ => 100,’maxheight’ => 200))//限制图片的长宽高
->addValidator ( ‘Count’, false, array(‘min’ => 1, ‘max’ => 3) );//上传文件数量
if (!$adapter->receive ()) {//当上传出错时,提示出错信息
$messages = $adapter->getMessages ();//检测
echo ‘<pre>’;
print_r($messages);
echo implode ( “\n”, $messages );
}else{
echo ‘上传成功!’;
}
}

2.upload.phtml模板文件中写上下面的代码:

<form enctype=”multipart/form-data” action=”http://localhost/zendframework/index/upload” method=”POST”>
选择: <input name=”uploadedfile” type=”file” />
<input type=”submit” value=”上传” />
</form>

发表评论

电子邮件地址不会被公开。 必填项已用*标注