开发者

Codeigniter image upload

开发者 https://www.devze.com 2023-02-17 19:12 出处:网络
I have a problem that if the user doesn\'t select an image he can press the \"submit\" bottom and then it edits the name in the database to nothing (because there is no image) so his profile picture i

I have a problem that if the user doesn't select an image he can press the "submit" bottom and then it edits the name in the database to nothing (because there is no image) so his profile picture is blank and I don't want that. I want to show a error that the user didn't select an image.

My other problem is when the user uploads a new image how can I delete the old one from the server?.

Here is my of my code:

view

    <?php echo form_open_multipart('profil/uploadImg'); ?>
    <div style="float:none; margin-bottom:5px;" id="profil_billed">
        <img width="200" height="200" src="<?php echo base_url(); ?>images/users/<?php echo $query->profile_picture; ?>" alt="">
    </div><!-- profil_billed -->
    <?php echo form_upload('userfile'); ?><br><br>
    <?php echo form_submit('upload', 'Upload billed', 'class=bottom'); ?>
    <?php echo form_hidden('username', $this->session->userdata('username')); ?>
    <?php echo form_close(); ?>

model

functio开发者_StackOverflow社区n addProfileImg()
{
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000,
        'encrypt_name' => true
    );

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

    $config = array(
        'source_image'    => $image_data['full_path'],
        'new_image'       => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width'           => 200,
        'height'          => 200,
        'encrypt_name'    => true,
        'max_size'        => 2000
    );

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


    # Ret profil billed navn #

    //$data = $this->upload->data('file_name');
    //print_r($data);

    $file_array = $this->upload->data('file_name');
    $profilBilledNavn['profile_picture'] = $file_array['file_name'];

    $this->db->where('username', $this->input->post('username'));
    $this->db->update('users', $profilBilledNavn);


}

controller

function uploadImg()
{
    $this->form_validation->set_rules('upload', 'upload',
        'required');
    $this->form_validation->set_rules('userfile', 'userfile',
        'required');

    if($this->form_validation->run() != true)
    {
        $this->load->model('profil_model');
        $this->profil_model->addProfileImg();
        redirect('profil/indstillinger/'.$this->session->userdata('username'));
    }
}


        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
                       // error ..show it to user

        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
                        // save to db

        }

and for deleting you have to use unlink() and then you may also have to delete it from db. you can use a simple getwhere .. example :

$config['query'] = $this->db->get_where('images','some id here may be');


You should do it with javascript

function validate() {

var extensions = new
Array("jpg","jpeg","gif","png","bmp");

// Alternative way to create the array
var extensions = new Array(){
extensions[1] = "jpg"; extensions[0] = "jpeg"; extensions[2] = "gif";
extensions[3] = "png"; extensions[4] = "bmp"; var image_file = document.form.image_file.value;
var image_length = document.form.image_file.value.length;
var pos = image_file.lastIndexOf('.')
+ 1; var ext = image_file.substring(pos, image_length);
var final_ext = ext.toLowerCase();

for (i = 0; i < extensions.length; i++)
{
if(extensions[i] == final_ext) {
return true;
}
}
alert("You must upload an image file with one of the following extensions: "+ extensions.join(', ') +"."); return false;
}

that way you wont need to do a request to the server.

** this code is from http://www.bitrepository.com/validating-an-image-upload.html


CI's file uploader has some basic error handling - from the docs

    if ( ! $this->upload->do_upload()) // here
    {
        $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);
    }

I imagine if the file is empty it will error.

As for deleting the image, I imagine you have the path of the image stored in the database:

You can use unlink():

$path = '/path/to/my/img/upload/001.jpg'; // wherever you store this value.

unlink($path); // returns true/false on success
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号