0

How to store Image Path In Database and retrieve from frontend

public function fanbookAction(){
        $img = ''.$this->getRequest()->getPost('img');
                if(isset($_FILES['img']['name'])) 
                {
                    try{
                        $uploader = new Varien_File_Uploader('img');
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // Your Format Here
                        $uploader->setAllowRenameFiles(true);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media').DS.'FanBook'.DS;
                        $uploader->save($path, $_FILES['img']['name']);
                        $img =$path. $_FILES['img']['name'];

                        $collection = Mage::getModel('fanbook/fanbook');
                        $collection ->setData('img',$img);
                        $collection->save();
                         $this->_redirectReferer();
                    Mage::getSingleton('core/session')->addSuccess('Done');
                    }

                    catch(Exception $e) {

                    }


                }
    }

Form.phtml

<form id="Fanbook" action="<?php echo Mage::getUrl('fanbook/index/fanbook') ?>" method="post"
      enctype="multipart/form-data">
      <input type="hidden" value="AQGISNWgIVMWTUnC" name="form_key">

        <input id="img" type="file" name="img">
            <input id="button" type="submit" value="Upload me!" name="submit">
    </form>
1

if you want to show in admin panel grid then use. but you have get destination file because of same name already exist then uploader give new name to that file.

 $uploader = new Varien_File_Uploader('img');
 $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); 
 $uploader->setAllowRenameFiles(true);
 $uploader->setFilesDispersion(false);
 $path = Mage::getBaseDir('media').DS.'FanBook'.DS;
 $destFile = $path.'/'.$_FILES['img']['name'];

 $filename = $uploader->getNewFileName($destFile);
 $uploader->save($path, $filename);
 $img ='FanBook/'.$filename;

  $collection = Mage::getModel('fanbook/fanbook');
  $collection ->setData('img',$img);
   $collection->save();

grid column

$this->addColumn('img', array( 'header' => Mage::helper('fanbook')->__('Image'), 'align' =>'left', 'index' => 'img',"renderer" =>"Companyname_Modulename_Block_Adminhtml_Renderer_Image", ));

you renderder Image.php

class Companyname_Modulename_Block_Adminhtml_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row)
    {
        $mediaurl=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
        $value = $row->getImg();
         return '<img src="'.$mediaurl.DS.$value.'"  style="width:100px;height:200px;text-align:center;"/>'; 
    }
}
  • I want to show image not url in admin grid – Magento 2 Apr 18 '16 at 12:32
  • this is not complete url this only image path after the media folder. – Qaisar Satti Apr 18 '16 at 12:34
  • Yes How to get Image in admin grid it shows url only – Magento 2 Apr 18 '16 at 12:35
  • paste your grid code then i will show you how to show image? – Qaisar Satti Apr 18 '16 at 12:37
  • $this->addColumn('img', array( 'header' => Mage::helper('fanbook')->__('Image'), 'align' =>'left', 'index' => 'img', )); – Magento 2 Apr 18 '16 at 12:38
0

I suggest you just store file name no need to store path

public function fanbookAction(){
        $img = ''.$this->getRequest()->getPost('img');
                if(isset($_FILES['img']['name'])) 
                {
                    try{
                        $uploader = new Varien_File_Uploader('img');
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // Your Format Here
                        $uploader->setAllowRenameFiles(true);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media').DS.'FanBook'.DS;
                        $uploader->save($path, $_FILES['img']['name']);
                        $img = $_FILES['img']['name'];

                        $collection = Mage::getModel('fanbook/fanbook');
                        $collection ->setData('img',$img);
                        $collection->save();
                         $this->_redirectReferer();
                    Mage::getSingleton('core/session')->addSuccess('Done');
                    }

                    catch(Exception $e) {

                    }


                }
    }

You can retrieve image on frontend as below:

<?php
$mediaUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).DS.'FanBook'.DS;
$collection = Mage::getModel('fanbook/fanbook')->getCollection();
foreach($collection as $child){
  echo "<img src='"'.$mediaUrl.$child->getImg().'"' alt='' />";
}
?>

Hope it helpful to you.

  • I tried Name also Not Store above code also not saving – Magento 2 Apr 18 '16 at 12:27
  • Didn't getting you can you please explain more – Prashant Valanda Apr 18 '16 at 12:30
0

This code is for saving

 if($data = $this->getRequest()->getPost()){
    $model = Mage::getModel('your/module');
    try {

       if(isset($_FILES['img']['name'])  and (file_exists($_FILES['img']['tmp_name']))) {

        $uploader = new Varien_File_Uploader('img');

            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything

         $uploader->setAllowRenameFiles(false);

         $uploader->setFilesDispersion(false);
          $path = Mage::getBaseDir('media') . '/';

         $uploader->save($path, $_FILES['img']['name']);

         $data['slideshow_image'] =$_FILES['img']['name'];

        } 
    $model->setData($data);
    Mage::getSingleton('adminhtml/session')->setFormData($data);

    try{
    $model->save();
    }
    catch(Exception $ex){
    die($ex->getMessage());
    }

For retrieve

<?php
$mediaUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).DS.'FanBook'.DS;
$collection = Mage::getModel('your/module')->getCollection();
foreach($collection as $child){
  echo "<img src='"'.$mediaUrl.$child->getImg().'"' alt='' />";
}
?>

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.