It looks like you're new here. If you want to get involved, click one of these buttons!
<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>
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);
}
Comments
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.
<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??????
Grrrrrrrrrrrrrrr........
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:
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-upload-files-with-codeigniter-and-ajax/
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?
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-upload-files-with-codeigniter-and-ajax/
http://www.phpletter.com/Our-Projects/AjaxFileUpload/