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…
- At time of login, grab the image from the database and store it to the filesystem.
- This will enable me to reference the images in the application from the file system to speed performance.
- 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:
Thanks in advance!