在codeigniter上传不起作用。可能是什么原因?

时间:2015-10-14 19:58:22

标签: php codeigniter

这是控制器,我没有错误

for (Iterator<String> it1 = proptypeconf.columnKeySet().iterator(); it1.hasNext();) {
        String type = it1.next();
        System.out.println(type);
        for (Iterator<Map.Entry<String, Double>> it2 = proptypeconf.column(type).entrySet().iterator(); it2.hasNext();){
            Map.Entry<String, Double> e = it2.next();
            if (e.getValue() < conflist.get(index-1)) {
                it2.remove();
            }
        }
    }

查看

public function admin_page(){
    if($this->input->post('add_product')){               
        if(empty($_FILES['userfile']['name'])){
            $data = array(
                'category' => $this->input->post('category') ,
                'product_name' => $this->input->post('name') ,
                'description' => $this->input->post('description'),
                'image' => 'no_image.jpg'
            );
        } else{                     
            $config['upload_path'] = './public/images/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|JPG';
            $config['max_size'] = '1000';
            $this->load->library('upload', $config);
            $this->upload->do_upload('userfile');
            echo 'uploaded :)';
            $data = array(
                'category' => $this->input->post('category') ,
                'product_name' => $this->input->post('name') ,
                'description' => $this->input->post('description'),
                'image' => $_FILES['userfile']['name'] 
            );           
        }      
        $this->load->model('products');
        $this->products->add_product($data);
    }
    $this->load->view('admin_page');
}

1 个答案:

答案 0 :(得分:1)

注意:要检查codeigniter 3的事情。

  

确保控制器和型号名称的首字母大写   import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.MapFragment; public class Track extends Fragment { MiddleMan mCallBack; MapFragment mapFragment; private GoogleMap googleMap; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallBack = (MiddleMan) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement ReqestConnect"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.track_display, container, false); mCallBack.DisplayHome(); mapFragment = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)); googleMap = mapFragment.getMap(); googleMap.setMyLocationEnabled(true); googleMap.getMyLocation(); return view; } } Admin.php

     

模型Products.php和class Admin extends CI_Controller {

CI2上传http://www.codeigniter.com/userguide2/libraries/form_validation.html

CI3上传http://www.codeigniter.com/user_guide/libraries/file_uploading.html

控制器

class Products extends CI_Model {

查看

将输入提交更改为按钮提交。

<?php

class Admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('upload');

        //$this->load->model('products'); 
    }

    public function index() {

    }

    public function admin_page() {

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('category', 'Category', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');

        if ($this->form_validation->run() == FALSE)  {

            $data['upload_errors'] = $this->upload->display_errors();

            $this->load->view('admin_page', $data);

        } else {


            /*
            * If userfile checks if.
            *
            */

            if (!empty($_FILES['userfile']['name'])) {

                /*
                *  
                *  Make sure your images upload path is on the main directory
                *
                *  application
                *  public 
                *  public > images
                *  system
                *  .htaccess
                *  index.php
                *
                */

                $config['upload_path'] = './public/images/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '3000';
                $config['max_width']  = '0';
                $config['max_height']  = '0';

                $this->upload->initialize($config);

                $field_name = 'userfile';

                if ( ! $this->upload->do_upload($field_name)) {

                    $data['upload_errors'] = $this->upload->display_errors();

                    $this->load->view('admin_page', $data);

                } else {

                    /*
                    * Using the variable below you are able to get codeigniter file upload info.
                    * Note: Codeigniter upload only made for one file
                    * $upload_data = $this->upload->data();
                    *
                    */

                    $upload_data = $this->upload->data();

                    $data = array(
                        'category' => $this->input->post('category') ,
                        'product_name' => $this->input->post('name') ,
                        'description' => $this->input->post('description'),
                        'image' => $upload_data['file_name'] 
                    );

                    $this->products->add_product($data);

                    $data['upload_errors'] = '';

                    $this->load->view('admin_page', $data);
                }


                /*

                    If no image isset

                */

            } else {

               echo "No File Isset";


                $data = array(
                    'category' => $this->input->post('category') ,
                    'product_name' => $this->input->post('name') ,
                    'description' => $this->input->post('description'),
                    'image' => 'no_image.jpg'
                );


                $this->products->add_product($data);

                $data['upload_errors'] = '';

                $this->load->view('admin_page', $data);

            }

        }
    }
}

我会使用codeigniter&#39; <div class="container"> <?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?> <?php if ($upload_errors) {?> <div class="alert alert-info"><?php echo $upload_errors;?></div> <?php }?> <?php echo form_open_multipart('admin/admin_page');?> <div class="form-group"> <label>Product name</label> <input type="text" name="name" class="form-control" value="<?php echo set_value('name');?>" size="50"> </div> <div class="form-group"> <label>Category</label> <input type="text" name="category" value="<?php echo set_value('category');?>" class="form-control" size="50"> </div> <div class="form-group"> <label>Description</label> <textarea class="form-control" name="description" rows="3"></textarea> </div> <div class="form-group"> <label>Image</label> <input type="file" name="userfile" size="20"> </div> <div class="form-group"> <button type="submit" class="btn btn-default">Save</button> </div> <?php echo form_close();?> </div> http://www.codeigniter.com/user_guide/helpers/form_helper.html

一切尽力而为。