开发者

codeigniter image reader

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 Codeig开发者_开发百科niter 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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜