Welcome Guest, Not a member yet? Register   Sign In
Form validation and file upload
#1

[eluser]someone Smile[/eluser]
Hello,

I would like to create some upload script where the user will enter title and description of file which will be uploaded, but I have problem.

Controller:
Code:
// some form validation variables
  
   if (isset($this->input->post('submit')) && $this->form_validation->run() == TRUE)
   {
    $config = array(
     'allowed_types' => 'jpg|jpeg|png|gif',
     'upload_path' => realpath(APPPATH . '../files'),
     'max_size' => 3000,
     'max_height' => 1024,
     'max_width' => 1024,
     'remove_spaces' => TRUE
    );
    $this->load->library('upload', $config);
    /* IF THE FILE WILL BE UPLOADED INSERT DATA FROM FIELDS IN DATABASE
                                $this->Insert->add_file();
    $this->session->set_flashdata('file_uploaded', TRUE);
    $data['fuploaded'] = $this->session->flashdata('file_uploaded');
    $this->load->view('upload/scheme', $data);*/
   }
   else
   {
    $this->load->view('upload/scheme', $data);
   }

This is not finished, because I don't know how to correctly open form - with form_open() or form_open_multipart(), to get all data checked for validation and after that to insert into database.

What do I have to do?

Thanks in advance! :-)
#2

[eluser]FightingMan[/eluser]
Maybe you can see the userguid.
you want to use form and form_validation helper,at first you must load them.
Also I autoload them,you can set in /application/config/autoload.php,except the file_upload library,in fact you can do it if you must use it many times.
you should see here:http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
http://ellislab.com/codeigniter/user-gui...ading.html
http://ellislab.com/codeigniter/user-gui...elper.html
Good Luck with you!Smile

#3

[eluser]someone Smile[/eluser]
Ok, I find soulution:
Code:
// some form validation variables
  
   if ($this->form_validation->run() == TRUE)
   {
    $config = array(
     'allowed_types' => 'jpg|jpeg|png|gif',
     'upload_path' => realpath(APPPATH . '../files'),
     'max_size' => 3000,
     'max_height' => 1024,
     'max_width' => 1024,
     'remove_spaces' => TRUE
    );
    $this->load->library('upload', $config);
       if ($this->upload->do_upload())
       {
          $this->Insert->add_file();
          $this->session->set_flashdata('file_uploaded', TRUE);
          $data['fuploaded'] = $this->session->flashdata('file_uploaded');
          $this->load->view('upload/scheme', $data);
       }
   }
   else
   {
    $this->load->view('upload/scheme', $data);
   }

But now I have new question. How to false form if there is error when uploading file?

Thanks! :-)
#4

[eluser]Stefan Hueg[/eluser]
Quote:But now I have new question. How to false form if there is error when uploading file?
Thanks! :-)

Put your upload-code into a seperate function like file_upload(), put this callback -function in as a rule (callback_file_upload) and let that upload function return FALSE on error or TRUE if no file was selected or upload has finished successfully.

Read the specific section in the documentation:
http://ellislab.com/codeigniter/user-gui...#callbacks
#5

[eluser]someone Smile[/eluser]
It's good idea, but I don't want to use callback function if this isn't necessary.

Is there any other solution like this example:
Code:
if ($uploaderror == TRUE)
{
   return false; //false form validation and get all values of fields back
}
#6

[eluser]Stefan Hueg[/eluser]
Of course you could do something like this
Code:
if ($this->form_validation->run() && $uploaderror == FALSE) //everything is fine
{
...
}

but this looks like a hack and it's better to stay with the form validation class.
#7

[eluser]someone Smile[/eluser]
Ok. Is it possible to save field's values and then if there is an error (I'm using flashdata+redirect) display them?
#8

[eluser]Stefan Hueg[/eluser]
Do you need the redirect? If not:
http://ellislab.com/codeigniter/user-gui...latingform

Else you could store them in your session data and repopulate it by yourself, but again you are bypassing the form validation class which makes it obsolete Wink
#9

[eluser]someone Smile[/eluser]
This is true but I think (for your first solution) it's not good to do very important (save uploaded file, resize image, load model and insert data in database) things with callback function.

Any other idea?
#10

[eluser]someone Smile[/eluser]
I get the solution:
Code:
// some form validation variables
  
   if ($this->form_validation->run() == TRUE)
   {
    $config = array(
     'allowed_types' => 'jpg|jpeg|png|gif',
     'upload_path' => realpath(APPPATH . '../files'),
     'max_size' => 3000,
     'max_height' => 1024,
     'max_width' => 1024,
     'remove_spaces' => TRUE
    );
    $this->load->library('upload', $config);
       if ($this->upload->do_upload())
       {
          $this->Insert->add_file();
          $this->session->set_flashdata('file_uploaded', TRUE);
          $data['fuploaded'] = $this->session->flashdata('file_uploaded');
          $this->load->view('upload/scheme', $data);
       }
       else
       {
          $data['uploaderror'] = $this->upload->display_errors(); // <- this line is the solution for my second problem
          $this->load->view('upload/scheme', $data);
       }
   }
   else
   {
    $this->load->view('upload/scheme', $data);
   }

Thanks for help anyway! :-)




Theme © iAndrew 2016 - Forum software by © MyBB