【问题标题】:Image uploading using codeigniter file uploading class使用codeigniter文件上传类上传图片
【发布时间】:2013-07-23 19:54:30
【问题描述】:

这不是一个如何上传图片的问题。我几乎成功地将图像上传功能添加到我的添加客户端功能中。当我尝试上传有效文件时效果很好.. 但是当我选择无效文件或更大的文件时,它会显示未定义的变量upload_data 和 codeigniter 数据库错误,其中img_pathNULL 它说Column 'img_path' cannot be null。为什么这个功能不起作用$this->upload->display_errors();。验证错误显示良好,但没有显示文件验证错误。

我正在使用 Codeigniter 和 hmvc

这是我的控制器

<?php

class Clients extends MX_Controller{

    function __construct(){
        parent::__construct();
        $this->load->model('mdl_clients');
    }

    function add(){
        $data['success'] = null;
        $data['errors']= null;
        if($_POST){
            $config_arr = array(
                'upload_path'   => './uploads/',
                'allowed_types' => 'gif|jpg|png',
                'max_size'      => '2048',
                'max_width'     => '1024',
                'max_height'    => '768',
                'encrypt_name'  => true,
                );         
            $this->load->library('upload', $config_arr);
            if (!$this->upload->do_upload()) {
                $data['errors'] = $this->upload->display_errors(); // this isn't working
            } else {
                $upload_data = $this->upload->data();
            }

            $config=array(
                array(
                    'field'=>'firstName',
                    'label'=>'First Name',
                    'rules'=>'required|max_length[15]|min_length[3]'
                ),
                array(
                    'field'=>'city',
                    'label'=>'City',
                    'rules'=>'required'
                ),
                array(
                    'field'=>'mobile_phone',
                    'label'=>'Mobile Number',
                    'rules'=>'required'
                ),
                array(
                    'field'=>'email',
                    'label'=>'Email',
                    'rules'=>'required|is_unique[clients.email]|valid_email'
                ),
            );
            $this->load->library('form_validation');
            $this->form_validation->set_rules($config);
            if($this->form_validation->run() == FALSE){
                $data['errors'] = validation_errors();
            }else{
                $data=array(
                    'img_path'=>$upload_data['file_name'],
                    'firstName'=>$_POST['firstName'],
                    'email'=>$_POST['email'],
                    'city'=>$_POST['city'],
                    'mobile_phone'=>$_POST['mobile_phone'],
                );
                $this->mdl_clients->add($data);
                $data['success'] = 1;
                $data['errors']= 0;
            }
        }
            $data['title'] = 'Add Client Database';
            $data['main_content'] = 'clients/add';
            echo Modules::run('templates/admin', $data);

    }

和我的视图文件..add.php

<? if($success==1) {?>
    <div class="alert alert-success">
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        Data Has been Updated ! 
    </div>
<? } ?>
<?php if($errors) { ?>
    <div class="alert alert-error" >
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <?=$errors?>
    </div>
<? } ?>

<?php $attributes = array('class' => 'form-horizontal');
echo form_open_multipart('clients/add', $attributes); ?>
    <fieldset>
        <!-- Address form -->

<h2>Client Information</h2>
<hr />
All Fields Marked with <span style="color: red;">*</span> is necessary .
    <hr />

        <!-- Upload input-->
        <div class="control-group">
            <label class="control-label">Upload<span style="color: red;">*</span></label>
            <div class="controls">
                <input name="userfile" name="userfile" type="file"
                class="input-xlarge">
                <p class="help-block"></p>
            </div>
        </div>

        <!-- firstName input-->
        <div class="control-group">
            <label class="control-label">First Name<span style="color: red;">*</span></label>
            <div class="controls">
                <input id="firstName" name="firstName" type="text" placeholder="First Name"
                class="input-xlarge" required>
                <p class="help-block"></p>
            </div>
        </div>
        <!-- Email input-->
        <div class="control-group">
            <label class="control-label">E-Mail<span style="color: red;">*</span></label>
            <div class="controls">
                <input id="email" name="email" type="text" placeholder="A Valid Email Address"
                class="input-xlarge" required>
                <p class="help-block"></p>
            </div>
        </div>
        <!-- City input-->
        <div class="control-group">
            <label class="control-label">City<span style="color: red;">*</span></label>
            <div class="controls">
                <input id="city" name="city" type="text" placeholder="City Name"
                class="input-xlarge" required>
                <p class="help-block"></p>
            </div>
        <!-- Mobile input-->
        <div class="control-group">
            <label class="control-label">Mobile Number<span style="color: red;">*</span></label>
            <div class="controls">
                <input id="mobile_phone" name="mobile_phone" type="text" placeholder="Current Mobile Phone Number"
                class="input-xlarge" required>
                <p class="help-block"></p>
            </div>
        </div>

        <!-- Button -->
        <div class="control-group">
            <div class="controls">
                <button class="btn btn-success">Add to Database</button>
            </div>
        </div>
    </fieldset>
</form>

【问题讨论】:

    标签: image codeigniter upload hmvc


    【解决方案1】:

    假设输入元素为:

    <input type="file" name="image" id="image">
    

    更改以下行:

    !$this->upload->do_upload()
    

    到:

    !$this->upload->do_upload('image')
    

    如果您遇到任何问题,请告诉我。

    更新

    如果您想将其发送到模板,请执行以下操作:

    if (!$this->upload->do_upload()) {    
        $error = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata('msg',$error['error']);
        redirect('controller_name/function_name','refresh');
    }
    

    让我知道这是否适合您。

    【讨论】:

    • 它不起作用...就像我说的有效文件上传工作但我认为它无法通过模板通过文件验证 $data['errors'] ..
    • 已检查..但不起作用...我将再次检查整个逻辑让我们看看是否有任何逻辑出错.. :(
    【解决方案2】:

    在进行表单验证时,您没有考虑是否存在上传错误。您应该检查是否存在上传错误,然后继续进行表单验证

    if($data['errors'] != '')
    {
       //do something, probably redirect back to the view and show the errors
    }
    else
    {
       if($this->form_validation->run() == FALSE)
       {
          $data['errors'] = validation_errors();
       }
       else
       {
           $data=array(
               'img_path'=>$upload_data['file_name'],
               'firstName'=>$_POST['firstName'],
               'email'=>$_POST['email'],
               'city'=>$_POST['city'],
               'mobile_phone'=>$_POST['mobile_phone'],
           );
           $this->mdl_clients->add($data);
           $data['success'] = 1;
           $data['errors']= 0;
        }
    }
    

    【讨论】:

    • 感谢 ans bro .. 在查看了我自己的代码几个小时后,我找到了解决方案。我将数据库字段从 not null 更改为 null 并且我正在向数据库发送 null 值..这就是为什么 ...删除 $upload_data = $this-&gt;upload-&gt;data(); 之后的 } 并将其粘贴在 $this-&gt;mdl_clients-&gt;add($data); 之后。效果很好
    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2011-06-08
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多