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;
Mahi
  • 23
  • 1
  • 5

2 Answers2

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() ?>"
dh47
  • 1,726
  • 1
  • 18
  • 31
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.