<form method="post" enctype=”multipart/form-data” action="__URL__/add">
内容:<textarea name="reason" rows = "5" cols = "50"> </textarea><br/>
日期:<input type="date" name="date" /><br/>
附件:<input type="file" name="image" />
<input type="hidden" name="studentid" value="{$vo.id}"/>
<input type="hidden" name="name" value="{$vo.name}"/>
<input type="submit" value="提交">
</form>调用的add方法如下: public function add()
{//将请求置入数据库
$usersM = M('Leave');
$result = $usersM->create();
if ( $result )
{
$return = $usersM ->add($result);
$id = $return;
}
if ( $return )
{
import("ORG.Net.UploadFile");
$upload = new UploadFile();//实例化上传类
$upload->maxSize = 65535 ; // 设置附件上传大小
$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//设置上传类型限制
$upload->savePath = './Public/leave/'; //设置上传路径限制
$upload->saveRule = $id . '_' . $this->_post('date') ;
$upload->uploadReplace = true;//若同名则覆盖
$result = $upload->upload();//保存上传文件,获取上传信息
if( !$result )
{
$this->error($upload->getErrorMsg());
}
else
{
$uploadList = $upload->getUploadFileInfo();//获取上传文件成功后的结果
$savename = $uploadList[0]['savename'];//获取保存的文件名
$result = $usersM ->where( array('id'=>$id) )->save( array('attach' => $savename ));//更新头像对应的文件名
if ( !$result )
{
$this->error($usersM ->getLastSql());
}
}
$this->success('保存成功!');
}
else
{
$this->error('插入数据时出错,请重试。'.$usersM ->getLastSql());
}
}基本上就是常规的使用方法,但是每一次的提示都是没有选择上传文件,而事实上已经选择了。(不用在意实现中的逻辑,这是为了测试恩...将来会改为允许不上传)感谢各位的帮助!
最佳答案