0

I am struggling with getting the full image path form image uploaded via custom field in backend

I am able to get only the name of the image file, but not the full media path

Here is the form code

  $fieldset->addField('featured_image', 'image', array(
        'name' => 'featured_image',
        'label' => 'Featured Image'
    ));

Here is the saveAction Method

 if ($data = $this->getRequest()->getPost()) {
        $model = Mage::getModel('blog/post');
 /* Code For Featured Image */


 if(isset($_FILES['featured_image']['name']) and (file_exists($_FILES['featured_image']['tmp_name']))) {
            try {
                $uploader = new Varien_File_Uploader('featured_image');
                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                $uploader->setAllowRenameFiles(false);
                $uploader->setFilesDispersion(false);   
                $path = Mage::getBaseDir('media') . DS .'blog'.DS;

                $cleaned_image_name = str_replace(' ', '_', $_FILES['featured_image']['name']);
                $dest_featured_img = $path.$cleaned_image_name;
                $new_fetaured_img_name = $uploader->getNewFileName($dest_featured_img);
               // $uploader->save($path, $_FILES['featured_image']['name']);
              $uploader->save($path, $new_fetaured_img_name);

              $data['featured_image'] = $new_fetaured_img_name;
            }
            catch(Exception $e) {
                echo $e->getMessage();
                exit;
            }
        }
$model->setFeaturedImage($data['featured_image']);
            $model->save();
}

Now, The image is being uploaded fine, but as we can see only name of the image file is getting saved to db. Hence When retriving in phtml file as below, only name is printed

Retrive image from db in phtml file

   <img src="<?php echo
     $post->getFeaturedImage();?>">

Output is (Current)

'<img src="uploaded_image.jpg">`

However it should be (Expected)

'<img src="http://sitename.com/media/blog/uploaded_image.jpg">'

I seriously do not want to hardcode the media/blog/ in phtml file as answered Here , nor would like to save the complete hardcoded url in db field as later on I'd be implenting cdn for images.

Also hardcoding would then fail if i intend to save image in catalog like style

media/blog/a/1/atest_image.jpg
media/blog/a/2/atest2-image2.jpg  

Then What is the correct way to get the full url of image by image name in this case.

I have seen various answers on stackoverflow / other site tutorials including followig but they don't talk about the full image url, but only the filename saved in db field.

Magento is not saving new field in database even after flush cache storage

http://www.devproblems.com/add-an-image-field-in-your-admin-plugin-by-example-aw-blog/

ad thumbnail to aw blog posts

1

Retrive image from db in phtml file like this:

<?php $imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'/blog/'.$post->getFeaturedImage();?>
<img src="<?php echo $imageUrl;?>">
  • Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) already comes with a trailing slash and when i use it with the setFilesDispersion(true) the output contains two slash e.g. http://sitename.com/media/ and then the image name from db which is /j/a/abc.jpg This mixed together give two trailing slash after media folder http://sitename.com/media//j/a/abc.jpg – echoashu Feb 8 '16 at 3:43
  • Try this : <?php $imageUrl = Mage::getBaseUrl().'/media'.$post->getFeaturedImage();?> <img src="<?php echo $imageUrl;?>"> – Abdul Feb 8 '16 at 4:46
  • OR try this:: <?php $imageUrl =Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).Mage_Core_Model_Store::URL_TYPE_MEDIA.$post->getFeaturedImage();?> <img src="<?php echo $imageUrl;?>"> – Abdul Feb 8 '16 at 4:53
0

the correct way to save the image with full start from blog/yourfile.jpg.

  1. first reason when you try to update the image it will automatically shown in edit admin from.

  2. for showing image on frontend

<img src="<?php echo $Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$post->getFeaturedImage();?>">

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.