2

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

In /var/www/html/magento16f/app/code/local/Custommodule/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);
}

I am not getting log (Mage::log) from controller

1 Answer 1

0

try to use bellow code

public function move_uploadedFileAction() {
    if (isset($_FILES['unverified-images']['name']) && $_FILES['unverified-images']['name'] != '') {
        try {
            $path = Mage::getBaseDir('media') . DS . 'mymodule' . DS .'model'.DS;
            if (!file_exists($path)) {
                mkdir($path, 777, true);
            }
            $uploader = new Varien_File_Uploader('unverified-images');
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            $uploader->setAllowRenameFiles(false);
            $uploader->setFilesDispersion(false);
            $destFile = $path.$_FILES['unverified-images']['name'];
            $filename = $uploader->getNewFileName($destFile);
            $uploader->save($path, $filename);

        } catch (Exception $e) {
            echo 'Error Message: ' . $e->getMessage();
        }
    }
}
2
  • i want to upload it in some folder may be at root so that i can share that link
    – inrsaurabh
    Commented Dec 15, 2017 at 13:46
  • is it possible to share the images from model folder ?
    – inrsaurabh
    Commented Dec 15, 2017 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.