views:

33

answers:

3

hello i am trying to use Codeigniter to build a simple user image gallery where a user can upload an image and then this will be auto displayed on another page.

I have the image uploading done as explain on the user guide on Codeigniter and this works fine dumps the image in a folder in the root just placing the URL in one table area and echoing this out works but this will not cover all image names.

what I would like to know is there a way of reading what the user has uploaded and auto storing this in the database to be echoed out.

if not what's the best practice way to do this?

ps new to codeigniter framework but quite familiar to PHP mysql many thanks.

A: 

I am not totally familiar with the Codeigniter framework, but from a standard PHP/MySQL perspective this can be done using a BLOB field with MySQL. Simply fread() the uploaded image (addslashes to the content before sending the data) and send that binary output to the BLOB field with a standard SQL query. After that, you will probably need to create a separate file to actually call the image back (PHP will need to reconstruct the image from the data). Familiarize yourself with the HTTP-HEADER "Content-Type:" and how it works for images.

For instance, if you stored a .jpeg image to the database, you will want to set

header("Content-type: image/jpeg");
echo $query_string_array['Image_Data_Row'];

This would need to be in its own file (or part of file ?image=some_img) so it can display only the image. But that is the basic concept of storing an image to the database; you simply break it down, store the information, and reconstruct it when you need it.

Regards,
Dennis M.

RageD
thanks but i really want to understand how to do it in codeigniter if no one else gets back to me ill give your go a try thanks Dennis,
I just looked this up, but have you taken a look at this thread? http://stackoverflow.com/questions/2775284/codeigniter-storing-an-image-in-the-database
RageD
A: 

When uploading data using the uploading class you can retrieve information about the uploaded file using the following:

$this->upload->data();

This will return an array containing the name, path, dimensions, etc.

Yorick Peterse
A: 

As Yorick alluded to: after uploading the file, you can access the file path with the $this->upload->data() method. Specifically, $this->upload->data('full_path'). After the upload, assign this to a variable, then send it to a model or insert the path directly into the db. This will be the full file path to the image on your system. If you want a http accessible path stored instead, you can do something like:

$this->load->helper('url');//load url helper if not autoloaded
$fileName = $this->upload->data('file_name');
$httpPath = base_url().'uploadFolder/'.$fileName;
$this->db->insert('image_table',array('path'=>$httpPath));

HTH

stormdrain