开发者

Magento "File was not uploaded"

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 "up开发者_运维技巧load 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!


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.


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)


I had some issues with adding images a while back, it turned out the flash image uploader was the culprit. I tracked down what swf file it was and replaced it with a newer version of Magento that I downloaded.

If that doesn't help here's a module that will allow you to upload images without the flash uploader. You may at least be able to ensure it's not a flash issue.


In my case, uploader.swf doesn't even contact the server and returns either Upload I/O Error or SSL Error.

I tried using Charles Proxy and "it works"!! i.e. when using a proxy, the uploader.swf now works. Without a proxy it doesn't.

Seems to me the problem is entirely in the SWF Uploader, not on the server at all.


verify your vhost include :

php_admin_value home_dir xxxxxxxxxxxxxx

So the upload_tmp_dir must be included in this root directory otherwise magento functions cant catch tmp_file

eg: your configuration vhost include home_dir /home/someone

and php.ini write upload files in /tmp

/tmp is not in dir home_dir and construct class function use file_exists() php cant read /tmp/

you must create /home/someone/tmp

and include in vhost configuration

php_admin_value upload_tmp_dir /home/someone/tmp

apache2 reload

hi


 if (isset($_FILES['cv']['name']) && $_FILES['cv']['name'] != '') {
            try {

                $uploader = new Varien_File_Uploader('cv');
                $uploader->setAllowedExtensions(array('doc', 'docx','pdf'));
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(false);
                $path = Mage::getBaseDir('media') . DS . 'jobs' . DS ;
                if(!is_dir($path)){
                    mkdir($path, 0777, true);
                }
                $uploader->save($path, $_FILES['cv']['name'] );
                $newFilename = $uploader->getUploadedFileName();
                echo "<br />new file name is = ".$newFilename;

            } catch (Exception $e) {
                $error = true;
                echo "<pre>";
                print_r($e);
                echo "</pre>";
            }
        }


I would check also the upload_max_filesize with phpinfo(). For me it was set to 2M and my file had 3M , and changing that, fixed my issue with the error File was not uploaded


  • Change browser, computer, clear local cache and cookies etc.
  • Check permissions for /media/ folder (tried 777 and 755).
  • Read and implemented changes to .htaccess for GoDaddy servers as explained by the .htaccess file in Magento root
  • Disabled store view, flushed cache, logged out from Magento, deleted cache from /var/cache/ and then re-enabled store view, cleared cache again
  • Uploaded and called an earlier version of Prototype.js
  • Uploaded a local version of jQuery as I read that Prototype and jQuery can conflict and this was the suggested solution
  • Checked that the GD extension is installed in my server's PHP build.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜