PHP SOLUTIONS

This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Saturday 30 March 2013

Magento : Upload the product image from front-end to back-end

Hi,

Here I have to upload the product image in Magento admin.


1)  /* Image upload and move to product_upload folder in media */       

     $absolute_path = Mage::getBaseDir('media') . DS .('product_upload_directory_name');
     $relative_path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
            // File Upload
            $files = $_FILES['File_name']['name'];
            if(isset($files) && $files != '')
           {  
                try
                {
                      if(file_exists($absolute_path.DS.$files))
                     {
                           $var = rand(0,99);
                           $files = $var.'_'.$files;
                      }
                      // Starting upload
                      $uploader = new Varien_File_Uploader('File_name');
                      //Here 4 extention would work
                      $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                      $uploader->setAllowRenameFiles(false);
                      //false -> get the file directly in the specified folder
                      //true -> get the file in the product like folders /media/catalog/product/file.gif
                      $uploader->setFilesDispersion(false);
                       //We set media as the upload dir
                       $uploader->save($absolute_path, $files);
                }
                catch(Exception $e)
               {
                      Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                }
                // Your uploaded file Url will be
                $file_url = $files;
                                                                   $mypath=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'product_upload/'.$file_url;
          Mage::getSingleton('core/session')->setFrontfilelogo($file_url);
      }
    //// End of Image Upload    

Enjoy...... This is working for me in Magento Ver 1.7.0.2