Magento "File was not uploaded"

25,653

Solution 1

If you check the XHR response in a debugger, you'll see this {"error":"File was not uploaded.","errorcode":666}

This error comes from Varien_File_Uploader::__construct() in lib/Varien/File/Uploader.php

Here are the important parts

<?php

class Varien_File_Uploader
{
    /**
     * Uploaded file handle (copy of $_FILES[] element)
     *
     * @var array
     * @access protected
     */
    protected $_file;

    const TMP_NAME_EMPTY = 666;

    function __construct($fileId)
    {
        $this->_setUploadFileId($fileId);
        if(!file_exists($this->_file['tmp_name'])) {
            $code = empty($this->_file['tmp_name']) ? self::TMP_NAME_EMPTY : 0;
            throw new Exception('File was not uploaded.', $code);
        } else {
            $this->_fileExists = true;
        }
    }
}

Looking back up the trace you see this is called

$uploader = new Mage_Core_Model_File_Uploader('image');

Which is extended from the Varien class, so the Varien_File_Uploader::_setUploadFileId($fileId) will construct the $this->_file array based on the key image, in this case.

So now the problem is why is $_FILES['image']['tmp_name'] empty?

I checked the 'error' field by temporarily changing the exception to

throw new Exception('File was not uploaded. ' . $this->_file['error'], $code);

I got 7, which is Failed to write file to disk. which means it's a permissions issue. Do a phpinfo() to check where your upload_tmp_dir is set to and make sure it's writable.

In my case, I was out of file space in the /tmp dir.

Solution 2

The exact exception/error-message your'e reporting doesn't show up in Magento's source code as a string, so I'm not 100% sure I'm pointing you in the right direction here.

That said, most uploads in magento are handled by the save method on an instantiated object of the Varien_File_Uploader class.

File: lib/Varien/File/Uploader.php
public function save($destinationFolder, $newFileName=null)
{
    $this->_validateFile();

    if( $this->_allowCreateFolders ) {
        $this->_createDestinationFolder($destinationFolder);
    }

    if( !is_writable($destinationFolder) ) {
        throw new Exception('Destination folder is not writable or does not exists.');
    }

    $result = false;

    $destFile = $destinationFolder;
    $fileName = ( isset($newFileName) ) ? $newFileName : self::getCorrectFileName($this->_file['name']);
    if( $this->_enableFilesDispersion ) {
        $fileName = $this->correctFileNameCase($fileName);
        $this->setAllowCreateFolders(true);
        $this->_dispretionPath = self::getDispretionPath($fileName);
        $destFile.= $this->_dispretionPath;
        $this->_createDestinationFolder($destFile);
    }

    if( $this->_allowRenameFiles ) {
        $fileName = self::getNewFileName(self::_addDirSeparator($destFile).$fileName);
    }

    $destFile = self::_addDirSeparator($destFile) . $fileName;

    $result = move_uploaded_file($this->_file['tmp_name'], $destFile);

    if( $result ) {
        chmod($destFile, 0777);
        if ( $this->_enableFilesDispersion ) {
            $fileName = str_replace(DIRECTORY_SEPARATOR, '/', self::_addDirSeparator($this->_dispretionPath)) . $fileName;
        }
        $this->_uploadedFileName = $fileName;
        $this->_uploadedFileDir = $destinationFolder;
        $result = $this->_file;
        $result['path'] = $destinationFolder;
        $result['file'] = $fileName;
        return $result;
    } else {
        return $result;
    }
}

Throw some debugging statements into this function to see if

  1. It's the one being called and is failing

  2. To figure out why it might be returning false (i.e., not uploading the file)

Share:
25,653
Admin
Author by

Admin

Updated on September 25, 2020

Comments

  • Admin
    Admin over 3 years

    I'm currently using the magento admin interface, trying to upload an image in the "manage products" and I get the error "file was not uploaded" after I browse the file and click "upload file". I've looked on other forums and the main solution I saw were to make sure that php.ini has the following lines...

    magic_quotes_gpc = off
    short_open_tag = on
    extension=pdo.so
    extension=pdo_mysql.so
    

    I have Windows/IIS with ISAPI_Rewrite. Is there a max file upload size that I can change somewhere. I'm uploading pictures from my local desktop of size ~100kb. help!