[eluser]behnampmdg3[/eluser]
Hello;
The simple script below uploads photos and successfully sends the "upload success" message to view.
But on
upload failure it doesn't send the message ($data) to the view which is very strange!
Because I can print_r($data) in controller and see the error message there! So how is it that I can see the variable in the controller but not in the view?
Thanks
Controller
Code:
class Gallery extends CI_Controller
{
public function index()
{
$data['title'] = "Gallary Page";
$this->load->vars($data);
if($this->input->post('submit'))
{
$this->do_upload();
}
$this->load_photos();
$this->view_things();
}
function do_upload()
{
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->display_errors());
$this->load->vars($data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->vars($data);
}
}
public function load_photos()
{
$this->load->model('load_photos_model');
$photos = $this->load_photos_model->check();
if($photos)
{
foreach($result as $row)
{
$data['photos'][]=$row->photo;
}
return true;
}
else
{
$data['photos_message'] = "There are no photos uploaded yet!";
$this->load->vars($data);
}
}
public function view_things()
{
$this->load->view('header_view');
$this->load->view('gallery_view');
$this->load->view('footer_view');
}
}
View:
Code:
<div id="common_div">
Gallery<br />
<?php echo $photos_message;?>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<form action ="<?php echo site_url();?>gallery" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>File</td>
<td><input class = "text-box" type="file" name="userfile" value="<?php echo set_value('email'); ?>"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Upload" name="submit" class="input_submit"/></td>
</tr>
</table>
</form>
</div>
On successful upload I get:
Quote: file_name: my_photo1.jpg
file_type: image/jpeg
file_path: C:/wamp/www/flatmates/uploads/
full_path: C:/wamp/www/flatmates/uploads/my_photo1.jpg
raw_name: my_photo1
orig_name: my_photo.jpg
client_name: detailed listing (2).jpg
file_ext: .jpg
file_size: 72.41
is_image: 1
image_width: 641
image_height: 768
image_type: jpeg
image_size_str: width="641" height="768"
On failure upload I get:
Quote: A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/gallery_view.php
Line Number: 5