Help with file upload

edited August 2012 in News & Announcements

I don't think this is a specific Fuel issue, but I do need help.

I have a form with a few input fields and a file upload field.

I don't get any errors about the upload, yet the file is not being uploaded.

I tried to find a solution in codeigniter forum but I can't seem to find the problem.

My form view has a field in it like this:
<form action="http://localhost/clarenssa/classifieds/adForm" method="post" accept-charset="utf-8" id="postForm" enctype="multipart/form-data"> .....A few input fields here didn't copy the code to save you reading.... <td><strong><label for="img">Upload an Image</label></strong></td> <td><input name="userfile" value="" type="file"></td>
My controeller:
function adForm() { $data['catId'] = $this->input->post('catId'); $data['subCatId'] = $this->input->post('subCatId'); $data['catTitle'] = $this->input->post('catTitle'); $data['subCatTitle'] = $this->input->post('subCatTitle'); $data['success'] = false; $data['errors'] = ''; $this->load->helper('form'); $this->load->library('form_validation'); if($this->input->post('posted')): $this->form_validation->set_rules('adType', 'Ad Type', 'callback_adTypeCheck'); $this->form_validation->set_rules('adTitle', 'Ad Title', 'trim|required|xss_clean'); $this->form_validation->set_rules('price', 'Price', 'trim|numeric|xss_clean'); $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean'); if ($this->form_validation->run()): if(!empty($_FILES['userfile']['name']))://Checking that the user is trying to upload a file $config['upload_path'] = './uploads/ads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '800'; $config['max_height'] = '600'; $this->load->library('upload', $config); if (! $this->upload->do_upload() ) $data['errors'] .= array('error' => $this->upload->display_errors()); else $data['upload'] = array('upload_data' => $this->upload->data()); else: $data['success'] = true; endif; else: $data['errors'] .= validation_errors(); endif; endif; $this->load->view('classifieds/postanad', $data); }
I have an uploads/ads folder in my site's root:

When I submit the form, it comes back with the success message but no file was uploaded.

Can someone please help?


  • edited 12:19PM
    Do you need to provide the full upload path? Also, I'm assuming that the folder allows files to be uploaded there.
  • edited 12:19PM
    I have changed the upload path to a full path, I have changed the way I display the errors and for testing, I removed the check: if(!empty($_FILES['userfile']['name']))://Checking that the user is trying to upload a file

    Now I get this error:
    You did not select a file to upload. Although I do select one.
  • edited 12:19PM
    Made sure my form has this field:
    <td><strong><label for="img">Upload an Image</label></strong></td> <td><input name="userfile" value="" type="file"></td>

    Tried to print: $_FILES['userfile']['name']
    Getting this error:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined index: userfile

    Filename: controllers/classifieds.php

    Line Number: 139

    Tried to print the whole $_FILES array in my view after submit, the array is empty.

    What am I doing wrong??????

  • edited 12:19PM
    Hmm... not sure what the issue is. Initially I thought maybe you weren't including the enctype="multipart/form-data" in your form tag but it looks like you are. What happens if you print_r($_FILES); in the controller after it posts.
  • edited 12:19PM
    It prints an empay array: Array()
  • edited 12:19PM
    I tried to submit the form normally and not with an Ajax call, then it works fine. I am trying to figure out what stops it from working with the Ajax call.

    My first Ajax call used serialize and after googling I understand that serialize does not send the file.

    I tried another way from a tutorial I found here:
    but it doesn't work either, still get the error that I did not select a file:

    $('#postForm').submit(function() { $.ajax({ url : this.action, secureuri :false, fileElementId :'userfile', dataType : 'json', data : { 'adType': $('#adType').val(), 'adTitle': $('#adTitle').val(), 'price': $('#price').val(), 'desc': $('#desc').val(), 'phone': $('#phone').val() }, success : function (data, status) { console.log(data); if(data.status != 'error') { $('#formBody').html('<p>Reloading files...</p>'); refresh_files(); } alert(data.msg); } }); return false; });

    Any idea how to upload the file with an Ajax call? maybe?
  • edited 12:19PM
    OK !!! found the problem, my file fields didn't have an id. Now everything works with these examples:
Sign In or Register to comment.