0

I followed this tutorial to add a featured image to my blog post

https://forum.aheadworks.com/featured-image-t3821.html

I added the following code

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);

              // setAllowRenameFiles(true) -> move your file in a folder the magento way
              // setAllowRenameFiles(true) -> move your file directly in the $path folder
              $uploader->setFilesDispersion(false);

              $path = Mage::getBaseDir('media') . DS ;

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

              $data['featured_image'] = $_FILES['featured_image']['name'];
           }catch(Exception $e) {

           }
        }

        // handle delete image
        else {
           if(isset($data['featured_image']['delete']) && $data['featured_image']['delete'] == 1)
              $data['image_main'] = '';
           else
              unset($data['featured_image']);
        }

to the file

app/code/community/AW/Blog/controllers/Adminhtml/AWblog/Manage/BlogController.php

and it works you can go to the post, add the featured image, click save, it saves, but if you click save again it wipes out the featured image,

I added

$model->setFeaturedImage($_FILES['featured_image']['name']);

just before

$model->save();

but isn't making a difference, any ideas would be greatly appreciated.

  • please check in your form 'enctype' => 'multipart/form-data' – Murtuza Zabuawala Sep 5 '16 at 13:23
  • didn't make a difference, I assume add 'enctype' => 'multipart/form-data' to the field set of featured image – MrJoshFisher Sep 5 '16 at 13:40
  • You have to add to this in your form – Murtuza Zabuawala Sep 5 '16 at 17:58
  • Its already added to the form :/ – MrJoshFisher Sep 6 '16 at 6:55
  • 1
    ini_set('file_uploads' , 'On' ); Write this line in your Form.php – Murtuza Zabuawala Sep 6 '16 at 9:38
1

please add this code before $model->save();

  if($id){
    if($data['featured_image'] == "" ){
       $data['featured_image'] = $model->getData('featured_image'); 
    }
  }

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.