简体   繁体   中英

Upload image codeigniter

I try to upload an image with CodeIgniter. The image has to be send to a folder and the path send to the database. When I do submit, it doesn't do anything. The var_dump of ($story_img) shows:

string(8) "/images/"

This is my code:

if ($this->input->server('REQUEST_METHOD') == 'POST') {
        $config['upload_path'] = '/images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '1000000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = FALSE;

        $this->load->library('upload', $config);
        $this->upload->do_upload('story_img');
        $image_path = $this->upload->data();
        $story_img = $image_path['full_path'];

        echo var_dump($story_img);

        $story_text = $this->input->post("story_text");
        $users_id = $this->input->post('users_id');


        $this->form_validation->set_rules('story_text', 'Story text', 'required');
        $this->form_validation->set_rules('story_img', 'Story image ', 'callback__image_upload');
        if ($this->form_validation->run() != FALSE) {

            $new_story = new Story_model();
            $new_story->Story_text = $story_text;
            $new_story->Users_id = $users_id;
            $new_story->Story_date = date('Y-m-d H:i:s');
            $new_story->Story_img = $story_img;


            $this->Story_model->addStory($new_story);
        }
    }

var_dump of $image_path

array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(8) "/images/" ["full_path"]=> string(8) "/images/" ["raw_name"]=> string(0) "" ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }

Are you sure that our form field name is story_img? Try to add conditionals:

if($this->upload->do_upload('story_img'))
{ 
   //your code...
}  

And try to display smth in conditional,to see if you pass it.

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

foreach ($image_path as $info) {
   $path=$info['full_path'];
}

echo $path;

您在表单标签中是否提到过enctype="multipart/form-data"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM