Skip to main content
1 of 6
inrsaurabh
  • 1.7k
  • 3
  • 27
  • 54

Move uploaded images to a folder in custom module

I am trying to move uploaded the image to a folder by a custom module.

In /var/www/html/magento16f/app/code/local/Hestabit/ReviewRating/controllers/IndexController.php

public function move_uploadedFileAction( ) {
    Mage::log($_FILES);

    $path = Mage::getBaseDir() . '/uploadfolder';
    Mage::log( $path );
    if (!file_exists($path)) {
        mkdir($path, 777, true);
    }
    try {
        $fname = $_FILES['unverified-images']['name'];
        $uploader = new Varien_File_Uploader('unverified-images');
        $uploader->setAllowedExtensions(array('png', 'gif', 'jpeg', 'docx'));
        $uploader->setAllowCreateFolders(true);
        $uploader->setAllowRenameFiles(false);
        $uploader->setFilesDispersion(false);
        $uploader->save($path, $fname);
        Mage::log( $uploader );

    } catch (Exception $e) {
        echo 'Error Message: ' . $e->getMessage();
        
    }

   $this->_redirectReferer();

  }

In /var/www/html/magento16f/app/design/frontend/base/default/template/reviewrating/signupformhb.phtml

''''''''''''''''''''''
   <input type="file"  multiple="multiple" name="unverified-images" value="Upload" />
 
 '''''''''''''''''''''''''

This function calls a js function which trigger some api on sussess of api return i want to move the uploaded images to a folder and then share the link.

In js file i call below function

uploadFileHb(params);

In /var/www/html/magento16f/app/design/frontend/base/default/template/reviewrating/signupformhb.phtml

function uploadFileHb(myFileObject) {
    var formData = new FormData();
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "<?php echo $this->getUrl('reviewrating/index/move_uploadedFile') ?>");
    // Send the file
    xhr.send(formData);
}
inrsaurabh
  • 1.7k
  • 3
  • 27
  • 54