2011-03-16 160 views
0

我有一个问题,如果用户没有选择图像,他可以按下“提交”底部,然后它将数据库中的名称编辑为空(因为没有图像),所以他的个人资料图片是空白的,我不想那样。我想显示用户没有选择图像的错误。Codeigniter图像上传

我的其他问题是当用户上传新图像时,我怎么能从服务器上删除旧的?

这里是我的代码我:

视图

<?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(); ?> 

模型

function 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); 


} 

控制器

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')); 
    } 
} 

回答

0

CI的文件上传有一些基本的错误处理 - 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); 
    } 

我可以想象,如果该文件是空的,就会报错。

至于删除图像,我想你必须存储在数据库中的图像的路径:

您可以使用unlink()

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

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

@Ross谢谢,但我得到这个错误:S致命错误:调用一个成员函数do_upload()一个非对象在 – ole 2011-03-16 20:16:36

+0

好,我dident加载上传LIB :p所以现在它的工作原理我认为 – ole 2011-03-16 20:24:21

+0

@Ross我现在尝试这个http://pastebin.com/LT3r60QC但它总是回显“错误”,如果我没有上传,如果我上传图像:S你可以看到什么错误? – ole 2011-03-16 20:57:55

0
 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 

     } 

和删除 你必须使用unlink() 然后你可能也必须从数据库中删除它。 你可以使用一个简单的getwhere .. 例如:

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

你应该做它用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;
}

这种方式,你不会需要做服务器的请求。

**这个代码是从http://www.bitrepository.com/validating-an-image-upload.html

+0

谢谢但如果用户禁用javascript:/ – ole 2011-03-18 14:37:22