Jump to content

Need Help With Extracting An Image From Mysql To Store To File System.


Slugger

Recommended Posts

Ok,

Here is my use-case for my project.

To facilitate the migration and ease of such migrations, I want to store my images in a database. Yes, I am aware of the downsides, but hear me out..

Ok, that code works just fine, I can upload an image to the database and display it in the application.

 

However.....I also have the need for performance to do the following...

1) At time of login, grab the image from the database and store it to the filesystem.

2) This will enable me to reference the images in the application from the file system to speed performance.

3) it also enables me to build / include these images into PDF files which do not work with the retrieval method I created to display the images in the application.

 

 

The db is a mysql database and my application is of course written in PHP.

Here is the code that displays the image that was done via a file upload, that is stored in the database and displayed in a webpage (this works!)

<?php

$agency_id = $this->session->userdata('agency_id');

$baseurl = base_url();

$source = $baseurl."index.php?/agencys/upload_header_logo/retrieve/".$agency_id;

?>

<img src="<?php echo ($source);?>"

 

 

Here is the code I use for the above "upload_header_log/retrieve" method.

function retrieve(){

$agency_id = uri_assoc ( 'retrieve' );

$query = "SELECT * FROM mcb_agency_header_images WHERE agency_id = '$agency_id'";

$result = mysql_query($query) or die(mysql_error());

// define results into variables

$name=mysql_result($result,0,"name");

$size=mysql_result($result,0,"size");

$type=mysql_result($result,0,"type");

$content=mysql_result($result,0,"content");

// give our picture the proper headers...otherwise our page will be confused

header("Content-Disposition: attachment; filename=$name");

header("Content-length: $size");

header("Content-type: $type");

echo $content;

}

 

I have tried to take the $content from the above and write it to a file, but it is always empty.

 

How can I "get" that picture out of a database, and create that image as a file in the local file system when the agency user logs in?

IE: reference it like this:

<img src=http://www.myserver/website/images/agency/1/header/header.jpg/>

 

Thanks in advance!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.