[eluser]sb05[/eluser]
Hi Guys,
I am really struggling adding an image upload in my form. WIth this code below I get an error: undefined variable full_file_path. But I did define it, so I don't know what's wrong. Hope you guys can help out.
Here is my controller: articles.php
Code:
function add(){
// set validation properties
$this->_set_fields();
// set common properties
$data['title'] = 'Add new Article';
$data['message'] = '';
$data['action'] = site_url('articles/addArticle');
$data['link_back'] = anchor('articles','Back to list of News Articles',array('class'=>'back'));
// load view
$this->load->view('banner_view', $data);
$this->load->view('left_column_view', $data);
$this->load->view('right_column_view', $data);
$this->load->view('articleEdit', $data);
$this->load->view('footer_view', $data); }
function addArticle(){
// set common properties
$data['title'] = 'Add new Article';
$data['action'] = site_url('articles/addArticle');
$data['link_back'] = anchor('articles','Back to list of Articles',array('class'=>'back'));
// set validation properties
$this->_set_fields();
$this->_set_rules();
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['upload_path'] = './uploads';
$this->load->library('upload', $config);
// $path_to_uploads='./application/uploads';
// $config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()){
$error = $this->upload->display_errors();
echo "The uploading is not working";
}else{
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
}
// run validation
if ($this->validation->run() == FALSE){
$data['message'] = '';
}else{
// save data
$article = array('header' => $this->input->post('header'),
'body' => $this->input->post('body'),
'image_url' => $full_file_path)
;
$article_id = $this->articleModel->save($article);
// set form input name="id"
$this->validation->article_id = $article_id;
// set user message
$data['message'] = '<div class="success">add new ARTICLE success</div>';
}
// load viewinclude 'person.php';
$this->load->view('banner_view', $data);
$this->load->view('left_column_view', $data);
$this->load->view('right_column_view', $data);
$this->load->view('articleEdit', $data);
$this->load->view('footer_view', $data); }
And my view: articleEdit.php
Code:
<form method="post" action="<?php echo $action; ?>">
<div id="p">
<table>
<tr>
<td width="30%">ID</td>
<td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->validation->article_id; ?>"/></td>
<input type="hidden" name="article_id" value="<?php echo $this->validation->article_id; ?>"/>
</tr>
<tr>
<td valign="top">Header<span style="color:red;">*</span></td>
<td><input type="text" name="header" class="text" value="<?php echo $this->validation->header; ?>"/>
<?php echo $this->validation->header_error; ?></td>
</tr>
<td valign="top">Body<span style="color:red;">*</span></td>
<td><input type="textarea" name="body" class="text" value="<?php echo $this->validation->body; ?>"/>
<?php echo $this->validation->body_error; ?></td>
</tr>
<tr>
<td valign="top">Image_url<span style="color:red;">*</span></td>
<td><input type="file" name="image_url" class="text" value="<?php echo $this->validation->image_url; ?>"/>
<?php echo $this->validation->image_url_error; ?></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</div><!-- END P-->
</form>
My database is working. Without the file upload everything works fine, and when I add the file upload code, the only thing my code does is store the name of the image in the image_url field in the database. But the image does not get uploaded in my uploads folder.
Hope you guys can help!