1

I have a module using which I need to upload an image. However Magento does not allow that.This is the stack trace that I have obtained

Unable to create directory 'http://localhost/rajeev/magento/media/rktsizechart/sc'.

#0 /home/rajeev/public_html/magento/lib/Varien/File/Uploader.php(183): Varien_File_Uploader->_createDestinationFolder('http://localhos...')
#1 /home/rajeev/public_html/magento/app/code/local/Rkt/Sizechart/controllers/Adminhtml/Cms/SizechartController.php(130): Varien_File_Uploader->save('http://localhos...', 'guitar.jpg')
#2 /home/rajeev/public_html/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Rkt_Sizechart_Adminhtml_Cms_SizechartController->saveAction()
#3 /home/rajeev/public_html/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('save')
#4 /home/rajeev/public_html/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#5 /home/rajeev/public_html/magento/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#6 /home/rajeev/public_html/magento/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#7 /home/rajeev/public_html/magento/index.php(87): Mage::run('', 'store')
#8 {main}

Beautiful. Path looks ok. Then why it does not work. So I debugged more. The last error is thrown from Varien_File_Uploader->_createDestinationFolder(). This is the code that we can see there.

private function _createDestinationFolder($destinationFolder)
{
    if (!$destinationFolder) {
        return $this;
    }

    if (substr($destinationFolder, -1) == DIRECTORY_SEPARATOR) {
        $destinationFolder = substr($destinationFolder, 0, -1);
    }

    if (!(@is_dir($destinationFolder) || @mkdir($destinationFolder, 0777, true))) {
        throw new Exception("Unable to create directory '{$destinationFolder}'.");
    }
    return $this;
}

No need to look full code. I am damn sure this is the code that makes problem here

 mkdir($destinationFolder, 0777, true);

Now I dont know why it returns false here. I have tried to put some urls directly over there. I can see that, if I am pointing to outside media directory, it returns true.

media directory has 777 permission. Then how the hell is this happening ?

In general I want to know,

How can I properly debug magento uploading error ?

What are the main reasons that cause magento file uploadation not successful ?

7
  • What is the value of $destinationFolder? why does it have seem to be a url Varien_File_Uploader->_createDestinationFolder('http://localhos...') – Renon Stewart Oct 20 '14 at 15:52
  • http://localhost/rajeev/magento/media/rktsizechart/sc this is the value of that variable. $path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'rktsizechart' . DS . 'sc' . DS ; this is the code that I am using to generate path. – Rajeev K Tomy Oct 20 '14 at 15:55
  • @programmer_rkt should you not be using getBaseDir and not getBaseUrl – David Manners Oct 20 '14 at 16:01
  • @R.S: Error was simple I used Mage::getBaseUrl instead of Mage::getBaseDir. Thanks for tracing it. Please post this as an answer – Rajeev K Tomy Oct 20 '14 at 16:06
  • @DavidManners : Ya didnt notice that. My bad :( – Rajeev K Tomy Oct 20 '14 at 16:07
6
 bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

$pathname should be the server path to the file and not a url

Try

$path = Mage::getBaseDir('media') . DS;

You should try using Varien_File_Uploader

/* Starting upload */
$uploader = new Varien_File_Uploader('filename');

// Any extention would work
$uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
$uploader->setAllowRenameFiles(false);

// Set the file upload mode 
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders 
//  (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);

// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS;
$uploader->save($path, $_FILES['filename']['name']);
1

What it looks like is your upload path is being generated using Mage::getBaseUrl this will provide you with a url and not a directory. If you replace this with Mage::getBaseDir then it will now work.

Your base path is http://localhost/rajeev/magento/media/rktsizechart/sc but should be a something like /var/www/magento/media/rktsizechart/sc

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.