Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Optional file upload issue!
#1

[eluser]sunnyd[/eluser]
Hi all,

I have a slight issue that I hope someone can help me with. I have a user profile system in place. I would like the photo upload to be optional. So far it works perfectly. The main problem that I am facing is that if a user upload an invalid file, an error is not being displayed. Instead the user is redirected to the main users list instead of taking him back to the form and displaying the fact that an invalid file has been selected.

Here's my code below:


Code:
$profile_photos_folder = './assets/media/images/profile_photos/';

            if (!file_exists ($profile_photos_folder)) {
                mkdir ($profile_photos_folder, 777, true);
            }

            $config['upload_path'] = $profile_photos_folder;
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = '2048';
            $config['max_width'] = '2048';
            $config['remove_space'] = TRUE;
            $config['encrypt_name'] = TRUE;



            if ($this->form_validation->run() === FALSE) {
                $this->display('admin/manage_user', $data, TRUE);
            } else {

                $_POST['id_usr'] = $id;

                //die(var_dump($_POST));

                if ( !empty($_FILES) AND !empty($_FILES['userfile']['name'])) {

                    if (!$this->upload->do_upload ()) {
                        $data['upload_error'] = $this->upload->display_errors ('<span>', '</span>');
                        $this->display ('admin/manage_user', $data, TRUE);
                    }
                    else
                    {
                        $this->upload->initialize ($config);

                        $file_info = $this->upload->data();

                        $thumbFolder = $_SERVER['DOCUMENT_ROOT'] . '/assets/media/images/profile_photos/' ;

                        // Create an array that holds the various image sizes
                        $configs = array();
                        $configs[] = array('source_image' => $file_info['full_path'], 'new_image' => $thumbFolder."thumbs/320/{$file_info['file_name']}", 'width' => 320, 'height' => 320);
                        $configs[] = array('source_image' => $file_info['full_path'], 'new_image' => $thumbFolder."thumbs/120/{$file_info['file_name']}", 'width' => 120, 'height' => 120);
                        $configs[] = array('source_image' => $file_info['full_path'], 'new_image' => $thumbFolder."thumbs/320x320/{$file_info['file_name']}", 'width' => 320, 'height' => 320, 'maintain_ratio' => FALSE);
                        $configs[] = array('source_image' => $file_info['full_path'], 'new_image' => $thumbFolder."thumbs/120x120/{$file_info['file_name']}", 'width' => 120, 'height' => 120, 'maintain_ratio' => FALSE);

                        // Loop through the array to create thumbs
                        //$this->load->library('image_lib');
                        foreach ($configs as $config) {
                            //$thumb = FCPATH.'assets/'.$this->input->post('photo_category') . '/' . $config['new_image'];
                            $thumb = dirname($config['new_image']);
                            if (!file_exists($thumb)) {
                                mkdir($thumb, DIR_WRITE_MODE, true);
                            }
                            $this->image_lib->thumb($config);
                            //$this->_thumb($config, $file_info['file_path']);
                            //$this->image_lib->thumb($config, $file_info['file_path']);
                        }

                        $_POST['photo_usr'] = $file_info['file_name'];
                        $result = $this->user_model->update_account_details($_POST);
                    }
                } else {
                    $result = $this->user_model->update_account_details($_POST);
                }

                if ($result) {
                    $this->session->set_flashdata('success', '<p>User account has been updated!</p>');
                    redirect(base_url() . 'admin/users');
                } else {
                    $this->session->set_flashdata('failure', '<p>There was a problem updating this user\'s account</p>');
                    redirect(base_url() . 'admin/users');
                }

Can anyone advise me as to what is wrong with the code.

Thanks

Update: Fixed my own problem....




Theme © iAndrew 2016 - Forum software by © MyBB