drblhw5731 2016-10-15 20:20
浏览 30
已采纳

上传Helper Codeigniter问题。 没有上传图片

No image being upload what am I'm doing wrong with the Upload Helper CodeIgniter. It's driving me crazy.

View

<?php echo form_open_multipart('http://work/npanel/users/save'); ?>
<div class="form-group">
    <?php echo form_label('Name'); ?>
    <?php 

    $data_name = array(
      'name' => 'name',
      'class' => 'form-control',
    );

    echo form_input($data_name);
    ?>
</div>
<div class="form-group">
    <?php echo form_label('Lastname'); ?>
    <?php 

    $data_lastname = array(
      'name' => 'lastname',
      'class' => 'form-control',
    );

    echo form_input($data_lastname);
    ?>
</div>
<div class="form-group">
    <?php echo form_label('Picture'); ?>
    <?php

    $data_upload = array(
      'type' => 'file',
      'name' => 'image'
    );
    echo form_upload($data_upload);
    ?>
</div>

<?php
$data_hidden = array(
  'id' => $id
);

echo form_hidden($data_hidden);
?>

<?php
$send = array(
  'class' =>'btn-primary',
  'type' =>'submit',
  'value' =>'Save'
);

echo form_submit($send);
?>

<?php echo form_close(); ?>

Controller

public function save($name, $lastname, $id, $image)
{
    $name = $this->input->post('name');
    $lastname = $this->input->post('lastname');
    $id = $this->input->post('id');
    $image = $this->input->post('image');

    $this->load->model('User_model');
    $last_id = $this->User_model->save($name, $lastname, $id, $image);

    $config['upload_path'] = '/assets/img/users/';
    $config['allowed_types'] = 'jpg|png|gif';
    $config['remove_spaces'] = TRUE;
    $config['max_size'] = '2048';

    $this->load->library('upload', $config);
    $this->upload->do_upload("image");
    $t = $this->upload->data;

    $this->load->model('Images_model');
    $this->Images_model->setImage($t, $last_id);

    redirect('http://work.com/npanel/users');
}

Does not save image 'assets / img / users' folder and does not generate the file name. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douhe6181 2016-10-15 21:03
    关注

    On view on your form_open_multipart('users/save') you only need the controller name and function

    You can use form validation if you want to http://www.codeigniter.com/user_guide/libraries/form_validation.html

    Make sure you have set your base_url in config.php

    config.php

    $config['base_url'] = 'http://work/npanel/';
    

    Controller function

    Filename Users.php first letter upper case only

    class Users extends CI_Controller {    
    
        public function __construct() {
            parent::__construct();
            $this->load->library('upload');
            $this->load->model('user_model');
            $this->load->model('images_model');
        }
    
        public function save($name,$lastname,$id,$image) {     
            $name = $this->input->post('name');
            $lastname = $this->input->post('lastname');
            $id = $this->input->post('id');
            $image = $this->input->post('image');   
    
            $last_id = $this->user_model->save($name, $lastname, $id, $image);
    
            $config['upload_path'] = './assets/img/users/';
            //$config['upload_path'] = FCPATH . '/assets/img/users/';
            $config['allowed_types'] = 'jpg|png|gif';
            $config['remove_spaces'] = TRUE;
            $config['max_size']    = '2048';
    
            $this->upload->initialize($config);
    
            $this->upload->do_upload("image");
    
            $t = $this->upload->data; 
    
            $this->images_model->setImage($t['file_name'], $last_id);
    
            redirect('users/save');     
    
        }
    
    }
    

    On view your forum open should only controller name and function you can also put folder name if in folder

    <?php echo form_open_multipart('users/save'); ?>
    
    <?php echo form_open_multipart('if_in_folder_name/users/save'); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R