简体   繁体   中英

codeigniter photo upload submit

My HTML:

<?php echo form_open_multipart(base_url() . 'user/' . $this->session->userdata['username'] . '/settings/picture'); ?>
    <input type="file" name="photo_data" id="photo_data" value="" />
    <input type="submit" value="Save" name="submit" class="button_ui fr" />
<?php form_close(); ?>

So the user goes to its settings page and clicks Picture Tab in the menu which then shows the above HTML and allows the user to update the picture.

I then went into my user controller and tried to check if the user is in the picture tab and print out the data being sent so i can continue coding the rest part... but the thing is that it doesnt print the picture that i select to upload...

My controller code:

if ($this->uri->segment(4) == 'picture'){
    if (isset($_POST["submit"])){
        print_r($_POST);
    }
}

Output:

Array ( [submit] => Save Changes ) 

As The Maniac pointed out, file uploads are stored in the $_FILES global, not $_POST. But you don't even need to use these with CodeIgniter (so long as it's one file upload you're doing). In your controller, you can use CI's build-in file upload class:

public function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    // Automatically finds your user's file in $_FILES
    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('upload_success', $data);
    }
}

If you're uploading multiple files, you'll need to do a foreach loop with $_FILES. Find out more about it in their documentation . Also, you can minimize your code in your view with something like:

<?php 
   echo form_open_multipart('user/'.$this->session->userdata['username'].'/settings/picture'); ?>
   echo form_upload(array('name'=>'photo_data','id'=>'photo_data'));
   echo form_submit('submit','Save');
   echo form_close(); 
?>

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