• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
view image

#1
[eluser]shiva478[/eluser]
Hi

I'm new to php and codeigniter. I was doing a tutorial on file uploading class in http://ellislab.com/codeigniter/user-gui...ading.html

I was able to insert the image path into my database. The only problem I have is that I want to view the images on the view and I'm not sure how I go about doing that.

This is my upload controller
Code:
<?php

class Upload extends CI_Controller {

function __construct()
{
  parent::__construct();
  $this->load->model('upload_model');
  $this->load->helper(array('form', 'url'));
}

function index()
{
  $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
  {
   $error = array('error' => $this->upload->display_errors());

   $this->load->view('upload_form', $error);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   $upload_info = $this->upload->data();
  
   $insert_data = array(
              'id' => $this->input->post('id'),
              'image_path' => $upload_info['file_path'],
              'content' => $this->input->post('content')
             );
   $this->db->insert('image', $insert_data);


   $this->load->view('upload_success', $data);
  
  }
}

function view(){
  
  $data['title'] = "Voluptuous Decadents";
  $data['heading'] = "Bras";
  $data['query'] = $this->db->get('image');
  
  //$this->load->view('layout');  
  $this->load->view('view', $data);
}
}
?>

This is my upload_model
Code:
<?php
class Upload_model extends CI_Model {

public function __construct()
{
  $this->load->database();
}

public function get_upload()
{


  $query = $this->db->get('image');

}
}
?>

This is my upload_form
Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />
<?php echo form_input('content', set_value('content', 'content'));?>
<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;
&lt;?php echo anchor('upload/view', 'view'); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

This is my upload_success view
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your file was successfully uploaded!</h3>

<ul>
&lt;?php foreach ($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>

&lt;?php endforeach; ?&gt;
</ul>

<p>&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;</p>




&lt;/body&gt;
&lt;/html&gt;

This is my view
Code:
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;


<h1>&lt;?php echo $heading ?&gt;</h1>

&lt;?php foreach($query->result() as $row): ?&gt;

<p>&lt;?php echo $row->content ?&gt;</p>

<img src="&lt;?php echo $row-&gt;image_path ?&gt;" />



&lt;?php endforeach; ?&gt;

&lt;body&gt;
&lt;/html&gt;

#2
[eluser]DarkManX[/eluser]
1. If you are new to php, dont start with a framework, just code in pure php without any frameworks. Starting with ci without knowing php you will just copy & paste the tutorial code without knowing whats really going on.
2. You got a systax error in your view, you forgot ";".
3. If (2) is not the source of trouble, that is the output when you call /upload/view?

#3
[eluser]shiva478[/eluser]
1. I'm trying to kill two birds with one stone. Before I posted my problem I did go through the coding so I do know what is more or less happening

2. The ; wasn't the problem.

3. When I call /upload/view I get the content, I don't get any images.

I have tried to get the images by putting
Code:
&lt;?php echo $row->image_path; ?&gt;
but then I only get the path to the image which is
Quote:C:/wamp/www/tutorial/uploads/

#4
[eluser]LuckyFella73[/eluser]
You only save the image path into db not the image name.
Your insert should look like:
Code:
$insert_data = array(
'id' => $this->input->post('id'),
'image_path' => $upload_info['file_path'],
'filename' => $upload_info['file_name'],// this is new
'content' => $this->input->post('content')
);
Your table needs a colomn named "filename" then.
In your view echo path and filename. You could even
just save the filename and set the path in a config file.
If you change the path in future (could happen) it's easier
to change the config instead of all the rows in your db.

#5
[eluser]shiva478[/eluser]
@LuckyFella73
I did what you said and my image doesn't load. I get the little box with a torn edge like one usually gets when you can't view a picture.

I changed
Quote:<img src="&lt;?php echo $row-&gt;image_path ?&gt;" />

into
Quote:<img src="&lt;?php echo $row-&gt;filename; ?&gt;" />

#6
[eluser]srpurdy[/eluser]
[quote author="shiva478" date="1345652369"]@LuckyFella73
I did what you said and my image doesn't load. I get the little box with a torn edge like one usually gets when you can't view a picture.

I changed
Quote:<img src="&lt;?php echo $row-&gt;image_path ?&gt;" />

into
Quote:<img src="&lt;?php echo $row-&gt;filename; ?&gt;" />
[/quote]

you need both the image path and the image filename.

Normally I don't save paths into the db. I think it's kind of pointless, and if you ever change folder structures that's just a huge pain. Better off making a config file with the image path in it, and only saving the file name into the db.

$config['image_file_path'] = 'path/to/myimages/';

make sure that config file is loaded in the controller. (or autoloaded if it's a global thing)

Then you can just do
Code:
<img src="&lt;?php echo base_url() . $this-&gt;config-&gt;item('image_file_path') . $row-&gt;filename;?&gt;" alt="" />

Unless of course there is some reason you need the path in the db. Like if the paths will be different. But even then you don't really need to save the path in the db. Like say you had a folder for each user or something. You can have a base patth to where all those folders are, and you can use a query to get the folder name based on username or something like that. So pretty much avoiding having to save it into the db for no reason really. DarkManX does make a good suggestion about trying pure php, but CI is probably the easier framework to get to grips with so that's a good thing.

Just remember the easier way is not always the best way to do something. Smile and other times it can be the complete opposite of that. Big Grin

#7
[eluser]DarkManX[/eluser]
[quote author="shiva478" date="1345641272"]1. I'm trying to kill two birds with one stone. Before I posted my problem I did go through the coding so I do know what is more or less happening[/quote]

You cant learn to play football without being able to run. i suggest you - once more - to practice on some pure php stuff.

#8
[eluser]LuckyFella73[/eluser]
@shiva478

if you managed it to upload your file and make an entry into your
database you shouldn't giv up - my opinion.

Please check the following points before going on:
1. is you file really uploaded (look into your folder to get sure)
2. is the entry in your db made and the values correct (filename is the same like the uploaded file)?

If yes come back here and we get you image to display - hopefully Wink

#9
[eluser]shiva478[/eluser]
@LuckyFella73

1. yep the file is uploaded to the folder
2. yep the db entry and the filename are the same

#10
[eluser]LuckyFella73[/eluser]
ok that sounds good!

If you put that into your view you should see the image:

Code:
<img src="&lt;?php echo $row-&gt;image_path.'/'.$row-&gt;filename; ?&gt;" />
// OR:
<img src="&lt;?php echo base_url().'uploads/'.$row-&gt;filename; ?&gt;" />
// assuming your root folder is "tutorials"

I assume that you named the new column in your tabel "filename"
where you save the upload files name.

Hope that works


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.