Skip to main content
2 of 6
added 474 characters in body
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

  function write_review() {  
    var form = document.getElementById('hb_writereview_user');
    var url = "http://somewebsite.com?api";
    var params = form.serialize();
    httpRequest (params, url, 'POST', function (response) {    
        if(response.error) {
            document.getElementById('hb_show_message_review').innerHTML=response.message;
        } else {
                uploadFileHb(params);

        }
    });
}

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

<script type="text/javascript">
//<![CDATA[

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