1

I uploaded image file name called !!!.png it stored in Database but in Media it stored file.png i don't know what is the problem

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;
                        $destFile = $path.'/'.str_replace(' ','_',$_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();

                        $this->_redirectReferer();
                        Mage::getSingleton('core/session')->addSuccess('Your Request has been Completed Successfully');
                    }

                    catch(Exception $e) {

                         $this->_redirectReferer();
                        Mage::getSingleton('core/session')->addSuccess('Upload Error');

                    }


                }
    }

Updated

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;
                        $destFile = $path.'/'.str_replace(' ','_',$_FILES['img']['name']);
                        //$filename = $uploader->getNewFileName($destFile);
                        $fileName = preg_replace('/[^a-z0-9_\\-\\.]+/i', '_', $fileName);
                        if (preg_match('/^_+$/', $fileInfo['filename'])) {
    $fileName = 'file.' . $fileInfo['extension'];
}

                        $uploader->save($path, $filename);
                        $img ='FanBook/'.$filename;
                        $collection = Mage::getModel('fanbook/fanbook');
                        $collection ->setData('img',$img);
                        $collection->save();

                        $this->_redirectReferer();
                        Mage::getSingleton('core/session')->addSuccess('Your Request has been Completed Successfully');
                    }
2
  • change line $uploader->save($path, $filename) to if(!$uploader->save($path, $filename) )){ throw new Exception($this->__('There might be a problem with directory permission.')); } check it will throw exception. – Prashant Valanda May 3 '16 at 6:50
  • After adding this it shows blank page – Magento 2 May 3 '16 at 6:52
1

Problem is you cannot have an uploaded file called !!!.png.

When your code runs, the save() method of Varien_File_Uploader is called and it will sanitize the file name using $fileName = self::getCorrectFileName($fileName)

This method uses a regular expression to get rid of the unwanted characters such as the exclamation mark:

static public function getCorrectFileName($fileName)
{
    $fileName = preg_replace('/[^a-z0-9_\\-\\.]+/i', '_', $fileName);
    $fileInfo = pathinfo($fileName);

    if (preg_match('/^_+$/', $fileInfo['filename'])) {
        $fileName = 'file.' . $fileInfo['extension'];
    }
    return $fileName;
}

Step by step:

$fileName = preg_replace('/[^a-z0-9_\\-\\.]+/i', '_', $fileName);

The $fileName will be _.png after this line.

Then:

if (preg_match('/^_+$/', $fileInfo['filename'])) {
    $fileName = 'file.' . $fileInfo['extension'];
}

The $fileName will be file.png after this line.

To fix your problem you need to change your file name to something valid.

9
  • Where i need to add this in my controller or what – Magento 2 May 3 '16 at 7:12
  • @Kothari see my update – Raphael at Digital Pianism May 3 '16 at 7:12
  • Still i am not getting can you please update the answer – Magento 2 May 3 '16 at 7:14
  • @Kothari what are you not getting ? – Raphael at Digital Pianism May 3 '16 at 7:16
  • in which file i need to add this static public function getCorrectFileName($fileName) – Magento 2 May 3 '16 at 7:17

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.