2

How to get image from database and display it in magento?

my imgs.php is:

  $a = $_SESSION['id'];
  $ob = Mage::getModel('management/file')->getCollection();
        $res = $ob->addFieldToFilter('task_id',$a);
    //  $res = $ob->addFieldToFilter('comment_id',$id); 
        //$img = "";
        foreach($res as $val)
        {
            $img = $val->getFile();
            //$name = $val->getfilename();
            $Type = $val->getType();
            $type = "Content-type: ".$Type;
            header($type);
            print_r($img);
        }

in phtml:

    <img height="30px" width="30px" src="imgs.php?Id=<?php print_r($id);?>"/>

save image:

   $targetDir = Mage::getBaseDir('media') . DS . 'plupload' ;
   if (!file_exists($targetDir)) {
        @mkdir($targetDir);
    }       

    if (isset($_REQUEST["name"])) {
    $fileName = $_REQUEST["name"];
    } elseif (!empty($_FILES)) {
    $fileName = $_FILES["file"]["name"];
    } else {
    $fileName = uniqid("file_");
    }
    $filePath = $targetDir . DS . $fileName;
  • which image you want to fetch? – Manikandan Arunachalam Dec 19 '14 at 8:43
  • in custom module i store image in database that image i want to fetch – Mahi Dec 19 '14 at 8:51
  • just update the code how you save the image .. – Manikandan Arunachalam Dec 19 '14 at 9:34
  • Which location you save this images.You have not specify that yet. – Manikandan Arunachalam Dec 19 '14 at 10:06
0

Assuming you added images into a folder in

your_root_folder/media/your_custom_module/images Then fetch images adding this in your .phtml file.

<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'your_custom_module/images/'.$val->getFile() ?>"
  • o/p display like this ����\0JFIF\0\0\0\0\0\0��\0Ocad$Rev: 14797 $\0\0\0\0\0\0\0\0P��\0�\0 %# , #&\')*)-0-(0%()( @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@��\0 – Mahi Dec 19 '14 at 10:29
  • first thing did your images being saved in the folder?? – dh47 Dec 19 '14 at 10:31
  • yupp it is saved – Mahi Dec 19 '14 at 10:33
  • where did you save the images?? – dh47 Dec 19 '14 at 10:35
  • magento/media/plupload/ – Mahi Dec 19 '14 at 10:36
0

From your code, i cant get that where your file is stored, but if you store it in a directory, and you want to get all of that, then use below code to get all file list.

$datas = Mage::getModel('management/file')->getCollection();

foreach($datas as $data)
{
    Mage::log($data->getFile().'');//log this to find the location where you save file
}

So, the above code returns all files you have uploaded, again i dont know the directory where you save the images, so log this file location to find the directory you are using for store image.

Edit

$ob = Mage::getModel('management/file')->getCollection();
$res = $ob->addFieldToFilter('task_id',$a);
foreach($res as $val)
{
     <img src="<?php echo Mage::getBaseUrl()."media/plupload/".$val->getFilename(); ?>" />
}

Edit2 Change permission to your directory to 777.This will make your directory accessible if your url is correct and image is exist there.

  • not working.... – Mahi Dec 19 '14 at 10:30
  • @Mahi this is used to get the location where your images is stored. so just see the updated code and see the log.. – Manikandan Arunachalam Dec 19 '14 at 10:48
  • update the image url it shows on backend use firebug or something. – Manikandan Arunachalam Dec 19 '14 at 11:03
  • i want image in backend – Mahi Dec 19 '14 at 11:04
  • then what is the url in backend? – Manikandan Arunachalam Dec 19 '14 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.