0

here is my code to display image stored in GridFS to HTML page.

    $conn = new MongoClient('SHLY_DBSERVER');
    $db = $conn->newdb;
    $gridFS = $db->getGridFS('productImages');

       $image = $gridFS->find();
       $im_array=iterator_to_array($image);


        return $im_array;

by passing the $im_array to the view..

         foreach ($im_array as $key => $value) 
          {  
           $imageFile = $value->getBytes();

           $img=base64_encode($imageFile); ?>
    <img  style="width:300px;margin-left: 5em" class="img-responsive" src="data:image/jpg;charset=utf8;base64,<?php echo $img ?>"/><br>


     }

is it a good practise to display normal webpage images? should i go for file system storage ?

Telen Stanley
  • 310
  • 1
  • 6
  • 16
  • Point your image tag to a php script (using src) and pass that the file you want via $_GET. That php script reads the image data from grid FS, outputs the correct header and sends out the raw binary data for the image. Result is more web requests but without the increase in size of base64 encoded data. You can also have the php script send out the proper headers for caching. – Jonathan Kuhn Sep 13 '16 at 16:41
  • can you show me some code to understand? – Telen Stanley Sep 13 '16 at 17:00
  • http://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag, that shows how to do it using mysql, just replace fetching from a mysql db to fetching from the gridfs. It is essentially the same thing. – Jonathan Kuhn Sep 13 '16 at 17:06
  • @JonathanKuhn do you recommend to use GridFS by replacing filesystem? i need maximum performance security and flexibility. – Telen Stanley Sep 14 '16 at 06:14
  • I haven't used gridFS so I can't attest to it's abilities. However, I can say that I'm not usually a fan of storing documents/files in a database. It is useful if you need to index a document's contents or metadata, but if you don't need that, it is usually more of a pain that it is worth. Using the file system can offer just as much security and flexibility because those are usually built into your application, not the storage. You will likely see better performance from the file system, but we are talking microseconds so nothing to worry about. My personal preference would be filesystem. – Jonathan Kuhn Sep 14 '16 at 19:46
  • What I would do if I were working on a project like this (and I don't know the details, but for any image serving where I needed security) I would store the files in the file system, but outside the web root so they can't be accessed by the web server directly. Then store in the database a token to uniquely identify the image along with the file system path to the image. Someone requests a url with the unique token, use that to lookup the image path, and use php to send out the correct header and `readfile($pathToImage)`. – Jonathan Kuhn Sep 14 '16 at 19:52

1 Answers1

0

For images, js, css and attachment files, you should store files in file system only because your web-server manage client and server side caching.

JPatel
  • 103
  • 1
  • 6