Slugger Posted October 28, 2012 Share Posted October 28, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/269998-need-help-with-extracting-an-image-from-mysql-to-store-to-file-system/ Share on other sites More sharing options...
MDCode Posted October 28, 2012 Share Posted October 28, 2012 Storing images in a database is tedious. You're better off uploading them to a directory and inserting the link to the image in the database instead Quote Link to comment https://forums.phpfreaks.com/topic/269998-need-help-with-extracting-an-image-from-mysql-to-store-to-file-system/#findComment-1388286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.