Welcome Guest, Not a member yet? Register   Sign In
Form with image upload help
#1

[eluser]loopymonkey[/eluser]
ok I feel I'm close. I'm trying to combine Yannicks excellent form tutorial for codeigniter with codeigniter's uploader helper tutorial. I just want a simple form that uploads to a directory and passes along the data to my database including JUST the name of the file that was uploaded. Right now everything's working except the image uploading. The data I'm trying to collect is name, email, comments and the image file name as well as uploading the file.

My form is made up of 1. model for the database, 2. form controller, 3. form view. I am calling all my upload $config data from the config folder after making my own Upload.php file with all my settings. I know this works i've successfully used the upload tutorial in the user guide and was able to upload an image with that trick.

Any leads or help is much appreciated:

MODEL:
Code:
<?php
class Form_model extends Model
{
  function Form_model()
  {
    parent::Model();
  }
  function add_participant()
  {
    $YourName = $this->input->post('YourName');
    $email = $this->input->post('email');
    $comments = $this->input->post('comments');
    $userfile = $this->input->post('userfile');
    $this->db->query("INSERT INTO participants (id, YourName, email, comments, userfile) VALUES(NULL, '$YourName', '$email', '$comments', '$userfile')");
  }
  function list_participants()
  {
    $query = $this->db->query("SELECT * FROM participants");
    return $query;
  }
}
?>

FORM VIEW:

Code:
<div class="formArea">
        <div class="errors">&lt;?php echo $this->validation->error_string; ?&gt;</div>
         &lt;?php echo form_open_multipart('form/index'); ?&gt;
        
        <p class="formBold"><label for="YourName">YOUR NAME</label></span><br>
        &lt;?php echo form_input($YourName); ?&gt;
        
        <p class="formBold"><label for="email">YOUR EMAIL ADDRESS</label></span><br>
        &lt;?php echo form_input($email); ?&gt;
        
        <p class="formBold"><label for="comments">COMMENTS</label></span><br>
        &lt;?php echo form_textarea($comments); ?&gt;
                
        <p class="formBold"><label for="userfile">UPLOAD YOUR PHOTO</label></span><br>
        &lt;?php echo form_upload($userfile); ?&gt;            
        
  &lt;?php echo form_submit('submit', 'Submit'); ?&gt;
  &lt;?php echo form_close(); ?&gt;

FORM CONTROLLER:

Code:
&lt;?php
class Form extends Controller {

  function index()
  {  
    parent::Controller();

    # loaders
    $this->load->library('validation');
    $this->load->library('email');
    $this->load->helper(array('form', 'url', 'array'));
    $this->load->library('upload');
    
    $rules['YourName'] = "required";
    $rules['email'] = "required|valid_email";
    //$rules['image'] = "required";
    $this->validation->set_rules($rules);
    
    #Input and textarea field attributes
    $data['YourName'] = array('name' => 'YourName', 'id' => 'YourName');
    $data['email'] = array('name' => 'email', 'id' => 'email');
    $data['comments'] = array('name' => 'comments', 'id' => 'comments', 'rows' => 3, 'cols' => 48);
    $data['userfile'] = array('name' => 'userfile', 'id' => 'image');
    
    //$data = array('upload_data' => $this->upload->data());

    
    if ($this->validation->run() == FALSE)
    {
        $this->load->view('form_view', $data);
    }
    else
    {
    $YourName = $this->input->post('YourName');
    $email = $this->input->post('email');
    $comments = $this->input->post('comments');
    $userfile = $this->input->post('userfile');
    
    $this->load->view('formsuccess');
    $this->load->model('Form_model','', TRUE);
    $this->Form_model->add_participant();
    }
}
function results()
  {
      $this->load->helper(array('form', 'url', 'array'));
    $this->load->model('Form_model', '', TRUE);
    $data['query'] = $this->Form_model->list_participants();
    $this->load->view('results', $data);
  }
  

}
?&gt;
#2

[eluser]tonanbarbarian[/eluser]
Ok there are a couple of issues here.
Firstly in the form/index you are grabbing data from post and then not doing anything with it, the you load the Form_model and it grabs the same data from the post.

But your main problem is that while you are loading the upload library there is no code to actually do the upload

you are not setting any config options for the upload library and there is no call to $this->upload->do_upload()
#3

[eluser]loopymonkey[/eluser]
I put all my config options in a separate config file as directed by the user guide:

Quote:If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the upload.php, add the $config array in that file. Then save the file in: config/upload.php and it will be used automatically. You will NOT need to use the $this->upload->initialize function if you save your preferences in a config file.

I know those settings are all good and if i make a form with just an image upload only it works.

Any more insight or help much appreciated!
#4

[eluser]tonanbarbarian[/eluser]
are you calling
Code:
$this->upload->do_upload();
in your controller?
It is what does all of the upload processing
#5

[eluser]loopymonkey[/eluser]
Wow that was it, that worked! I just added that to my index controller, I was just missing that one line. Thanks tonan! But it's confusing to me as i don't use a function called do_upload, i know the upload tutorial in the user guide did though.

My only last thing I am trying to sort is how to make my $userfile collect the file_name data from the upload. it always collects nothing. If i discover it myself i will post but any hints appreciated! I'm becoming a happy codeigniter fan.
#6

[eluser]tonanbarbarian[/eluser]
the do upload function as you call it is a method in the upload library.
It is what does all of the work in the library

Most CI libraries need a method to be called so they can do some actually work
Just loading the library is seldom enough for it to work.

As for getting the data about the uploaded file
after do_upload has been called then $this->upload->data() returns an array with information about the file uploaded.
#7

[eluser]loopymonkey[/eluser]
My database keeps inputing 'Array' for the image after i post. I'm just trying to get just the filename of what is uploaded into my database. I understand in the upload user guide i can get this with file_name string, but i can't figure out where it goes. I would imagine I'm putting this in my model. $userfile = $upload_data['file_name']; ?
#8

[eluser]loopymonkey[/eluser]
I found out what i was missing i needed to add to my model:

Code:
$upload_data = $this->upload->data();
$userfile =    $upload_data['file_name'];

just wanted to post back in case anyone else has the same problem!
#9

[eluser]Bharani[/eluser]
dear i am used above code,,

but image not uploaded tell me how i do that plz ..very urgent
#10

[eluser]loopymonkey[/eluser]
Most hosts have limits on form file uploads, so the file you are trying to upload might be too large. Also keep in mind i was calling all my upload $config data from the config folder after making my own Upload.php file with all my settings. Hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB