this is going to be hazy... i have built a crud system for managing "offers" in a db with an upload field on both the create form and the update form.
when the user goes to edit i have the image displayed but if they want to upload a new one the form field is there in the update form to upload a new image when they submit the updates
the tricky part is if they don't want to $this->upload->do_upload() will dump out an error saying no image specified
To combat this I added on if(!empty($_POST['userfile'])) to only use the upload class if an image was specified, but when the form submits this seems to be empty always until it gets to the upload class function call.. heres some code
$userfile = $this->input->post('userfile');//i have tried $_POST as well
if( ! empty($userfile))
{
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo $this->upload->display_errors();
开发者_运维百科 $data['offer'] = $this->moffers->get_offer($this->input->post('id'));
$this->load->view('admin/admin_header');
$this->load->view('admin/edit',$data);
$this->load->view('admin/admin_footer');
}
any ideas on a solution of what is going wrong? the upload field works fine when i take out the empty() condition but wont work if the user doesnt specify a file to upload which they may not always want to do
<tr>
<td>Upload a new image</td>
<td><input type="file" name="userfile" size="40" /></td>
</tr>
Do it like this:
if(isset($_FILES['userfile']['tmp_name']) && !empty($_FILES['userfile']['tmp_name'])):
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo $this->upload->display_errors();
$data['offer'] = $this->moffers->get_offer($this->input->post('id'));
$this->load->view('admin/admin_header');
$this->load->view('admin/edit',$data);
$this->load->view('admin/admin_footer');
}
endif;
That's how I did it in one of my codeigniter projects.
I also created a codeigniter upload helper, can always be usefull ;) -> http://blog.cmstutorials.org/freebees/codeigniter-free-upload-helper
Try this:
if( ! empty($userfile))
{
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo $this->upload->display_errors();
}
}
$data['offer'] = $this->moffers->get_offer($this->input->post('id'));
$this->load->view('admin/admin_header');
$this->load->view('admin/edit',$data);
$this->load->view('admin/admin_footer');
精彩评论