用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4328|回复: 1
收起左侧

带文件上传的表单如何实现征求正解

[复制链接]
发表于 2008-5-8 16:57:32 | 显示全部楼层 |阅读模式
控制器部分

PHP复制代码
 
function webbase_add()
        {                
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';
                $config['file_name']=time();
               
                $this->load->helper(array('form', 'url'));
               
                $this->load->library('upload', $config);
 
 
                 if(!$this->upload->do_upload())
                  {
                           $error = array('error' => $this->upload->display_errors());
   
                           $this->load->view('adminweblogin/webbase_add', $error);
                }
                  else
                  {
                           $imgdata = array('upload_data' => $this->upload->data());  
                         
                        $data['logo']=$imgdata->file_name;
                        $this->load->model('Websitemodel');
                        $this->Websitemodel->webbase_add();
                        $this->load->view('upload_success', $imgdata);
                 }
        }
 
复制代码


模型部分:

PHP复制代码
 
function webbase_add()
        {
                $data['sitename'] = trim($_POST['sitename']);
                $data['content'] = trim($_POST['content']);
                $data['keywords'] = trim($_POST['keywords']);
                $data['copyright'] = trim($_POST['copyright']);
               
                if(!$this->upload->do_upload())
                {
                        $error = array('error' => $this->upload->display_errors());
   
                        $this->load->view('adminweblogin/webbase_add', $error);
                }
                else
                {
                        $imgdata = $this->upload->data();  
                       
                        $data['logo'] = $imgdata['file_name']; 
               
                        $this->db->insert('siteinfo', $data);
                }              
        }
 
复制代码


视图部分:
HTML复制代码
 
<?=form_open_multipart('/webbase/webbase_add'); ?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="88" height="24" align="right">网站名称:</td>
    <td><input name="sitename" type="text" id="sitename" size="30">
        <span class="redfont">*</span></td>
  </tr>
  <tr>
    <td height="24" align="right">网站标志图:</td>
    <td><input name="userfile" type="file" class="input" id="file" size="50"></td>
  </tr>
  <tr>
    <td height="24" align="right">网站介绍:</td>
    <td valign="top"><textarea name="content" cols="60" rows="5" id="content"></textarea>
        <span class="redfont">*</span> </td>
  </tr>
  <tr>
    <td height="24" align="right">网站关键字:</td>
    <td><input name="keywords" type="text" id="keywords" size="30" maxlength="80">
        <span class="redfont">*</span></td>
  </tr>
  <tr>
    <td height="24" align="right">网站版权声明:</td>
    <td><textarea name="copyright" cols="60" rows="5" id="copyright"></textarea>
      <span class="redfont">*</span></td>
  </tr>
  <tr>
    <td height="24" align="right"> </td>
    <td><input name="html_enable" type="checkbox" id="html_enable" value="1" checked>
选中允许HTML嵌套</td>
  </tr>
 
  <tr>
    <td height="24" align="right"> </td>
    <td><input type="submit" name="Submit" value="提交" onClick="return Check_Submit();">
      <input name="back" type="button" id="back" value="返回" onClick="javascript:history.back();"></td>
  </tr>
</table>
</form>
 
复制代码




以上代码可以实现文件上传并提交表单,但是本人觉得不够精练,而且有些理不清,希望各位能给出更好的解决方法

评分

参与人数 1威望 +5 收起 理由
Hex + 5 原创内容

查看全部评分

发表于 2013-9-12 17:36:22 | 显示全部楼层
你这个只有上传做了验证,其他的呢

本版积分规则