Welcome Guest, Not a member yet? Register   Sign In
Checking to see if there is file upload? [solved]
#1

[eluser]ywftdg[/eluser]
I currently have a system where a user can update their name and avatar. The upload works great with images, etc, but if I do not upload an image, I get an error saying no file was chosen. Sometimes the user might just update their web address. What can I use to check "if there is a file being uploaded, then run my upload process" ?

Any help would be greatly appreciated.
#2

[eluser]ywftdg[/eluser]
My code also:

Model

Code:
/////////////////////////////////////////////
    function update_mypage($id,$data)    {
        
        
            $data = array(
                        'dNickname' => $data[0],
                        'dBio' => $data[1],
                        'web' => $data[2],
                        'dBanner' => $data[3]
                );
            
        $this->db->where('designer.dDesignerId', $id);
        $this->db->update('designer', $data);
        
        
       }

Controller:
Code:
/////////////////////////////////////////////
    function mypage()    {
        $this->auth->restrict();
        
        $this->load->library('ftp');
        $this->load->library('validation');
        $this->load->library('upload');
        
        $id = $this->session->userdata('logged_user');
        
        $fields['nickname'] = 'nickname';
        $fields['bio'] = 'bio';
        $fields['web'] = 'web';
        $this->validation->set_fields($fields);

        
        if ($this->input->post('mypageupdate')) {        
        
                
                $dir = './assets/images/'.$id.'/';
                
                if (!is_dir($dir)) {
                    $theupload_path = mkdir('./assets/images/'.$id.'/', 0777);
                }
                
                //Upload Original Image
                $config['upload_path'] = $dir;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['remove_spaces']  = TRUE;  
                $config['max_size'] = '10000';
                $this->upload->initialize($config);
                
            
                if (!$this->upload->do_upload())    {
                    
                    $data['error'] = $this->upload->display_errors();
                
                } else {
                
                $data = array('upload_data' => $this->upload->data());
                $file = $data['upload_data']['file_name'];
                
                $filenew = rename($dir . $file, $dir . $id.'.jpg');
                
                $data = array(
                $this->input->post('nickname'),
                $this->input->post('bio'),
                $this->input->post('web'),
                $filenew
                );
                
                // Get the newly renamed file
                $filetarget = $dir . $id.'.jpg';
                
                //Create Thumbnail _t
                $config['image_library'] = 'gd2';
                $config['source_image'] = $filetarget;
                $config['create_thumb'] = TRUE;
                $config['thumb_marker'] = '_t';
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 57;
                $config['height'] = 40;
                $this->load->library('image_lib', $config);
                $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                
                //Create Banner _mypage
                $config['source_image'] = $filetarget;
                $config['create_thumb'] = TRUE;
                $config['thumb_marker'] = '_mypage';
                $config['quality'] = '95';
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 568;
                $config['height'] = 400;
                
                $this->load->library('image_lib', $config);
                   $this->image_lib->clear();
                $this->image_lib->initialize($config);
                $this->image_lib->resize();

                
                    
                $this->designer->update_mypage($id,$data);
                $this->session->set_flashdata('message', 'Mypage Updated!');
                
                redirect('designers/mypage');
                
            }
        }
        
        $data['query'] = $this->designer->get_designer($id);
        
        $pagedata['title'] = "Mypage Details";
        $pagedata['sectionstatus'] = $this->uri->segment(2, 0);
        $this->load->view('header_view', $pagedata);
        $this->load->view('mypage_view', $data);
        $this->load->view('_footer');
    }
#3

[eluser]Khoa[/eluser]
I got the same problem too, I want to make the image upload optional field. But using this code it always forces the image to be available and throws error when user did not choose any image (which is considered as valid option too). To work around this, I actually checks for the file name field returned by the file upload class to see if it contains any value.

Here is what I did to make an image field optional:

Code:
if (!$this->upload->do_upload("postImage"))
{
    $uploadData = $this->upload->data(); //This array contains many useful info, dump it out to see more
    
    //Check if user chose any file to upload. If they did not, just keep going
    if (trim($uploadData['file_name']) != ''){
        return $this->upload->display_errors();
    }
}

Khoa
#4

[eluser]Colin Williams[/eluser]
Code:
if (!is_array($_FILES) or !count($_FILES))
{
  // nothing to upload
}
#5

[eluser]Bramme[/eluser]
Code:
if ($_FILES['userfile']['error'] ! = 4) {
    // upload
}
That error specifically checks if a file was selected.
#6

[eluser]Unknown[/eluser]
Code:
if (strlen($_FILES["file-field-name"]["name"])>0) {

// upload

} else {

// dont upload

}




Theme © iAndrew 2016 - Forum software by © MyBB