CodeIgniter Forums
Problem with file upload on edit page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with file upload on edit page (/thread-13153.html)



Problem with file upload on edit page - El Forum - 11-12-2008

[eluser]opel[/eluser]
I have been working on a cms for a small site and having a problem with the edit page and file upload.

My edit page shows the currently uploaded file and option to upload a new file. THe problem occurs if I submit an edit page without a putting a new file in the "file" field as it tried to do the upload in the model even though I have put if not null. My code is below, could anyone advise where I am going wrong please ?

THanks

Code:
function update()
    {    
    

    
      $content = array('title' => $_POST['title'],
                      'category' => $_POST['category'],
                    'description' => $_POST['description'],
                  'order' => $_POST['order']);
    
    if ($_FILES['myfile'] != NULL)
    {
        $config['upload_path'] = './includes/uploads/';
        $config['allowed_types'] = 'gif|jpg';
        $config['max_size'] = '1000';
        $config['remove_spaces'] = true;
        $config['overwrite'] = true;
        //$config['max_width']  = '400';
        //$config['max_height']  = '400';
        $this->load->library('upload', $config);    
        
        if( ! $this->upload->do_upload('myfile'))
        {
            echo $this->upload->display_errors();
            exit();
        } else
        {
        
            // Thumbnail
            $data = array('upload_data' => $this->upload->data());
          $temp=$this->upload->data();
          
          $thumb['image_library'] = 'GD2';
          $thumb['source_image'] = './includes/uploads/'.$temp['file_name'];
          $thumb['new_image'] = './includes/uploads/thumb/'.$temp['file_name'];
          $thumb['maintain_ratio'] = TRUE;
          $thumb['create_thumb'] = TRUE;
          $thumb['quality'] = '100';
          $thumb['height'] = '80';
          $thumb['width'] = '80';
      
          $this->load->library('image_lib', $thumb);
          $this->image_lib->resize();
          
           //making a thumbnail
            $this->image_lib->clear();
            
            //insert to the DB


            $content['file'] = $temp['file_name'];


        }

        $this->db->where('id', $_POST['id']);
        $this->db->update($this->table, $content);
      }


    }



Problem with file upload on edit page - El Forum - 11-13-2008

[eluser]Flemming[/eluser]
you could try:
Code:
if($_FILES['myfile']['name'] != "")
or
Code:
if(!empty($_FILES['myfile'])



Problem with file upload on edit page - El Forum - 11-13-2008

[eluser]opel[/eluser]
neither of these worked I'm afraid Sad


Problem with file upload on edit page - El Forum - 11-14-2008

[eluser]Flemming[/eluser]
hmm .. ok, what about simply
Code:
if(!empty($_FILES))

or perhaps you could do

Code:
if(empty($_FILES['myfile'])) $noUpload = True;
if(!$noUpload) //then there IS an upload to handle
as I wonder if the trouble is that checking an empty array doesn't work?


Problem with file upload on edit page - El Forum - 11-14-2008

[eluser]El EmiZ[/eluser]
Try.
Code:
if ($_FILES['myfile']['error'] == UPLOAD_ERR_OK) {
// save to db code here
}

Hmmm... Just a small precaution, but make sure that in the database table your 'file' field is "NULL" and no "NOT NULL".

=)

Good luck.