簡體   English   中英

使用codeigniter文件上傳類上傳圖像

[英]Image uploading using codeigniter file uploading class

這不是如何上傳圖片問題。 我幾乎成功地設法將圖像上傳功能添加到我的添加客戶端功能中。 當我嘗試上傳有效文件時,它運行良好..但是當我選擇無效文件或更大的文件時,它會顯示未定義的變量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>

假設輸入元素為:

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

更改以下行:

!$this->upload->do_upload()

至:

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

如果您遇到任何問題,請告訴我。

UPDATE

如果要將其發送到模板,請執行以下操作:

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');
}

如果這對您有用,請告訴我。

在進行表單驗證時,如果存在上載錯誤,則不會考慮。 您應該檢查是否存在上載錯誤,而不是繼續進行表單驗證

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM