php Magento“文件未上传”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2670876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:17:14  来源:igfitidea点击:

Magento "File was not uploaded"

phpimagefile-uploadmagento

提问by John

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...

我目前正在使用 magento 管理界面,尝试在“管理产品”中上传图像,但在浏览文件并单击“上传文件”后出现错误“文件未上传”。我查看了其他论坛,我看到的主要解决方案是确保 php.ini 具有以下几行...

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!

我有带有 ISAPI_Rewrite 的 Windows/IIS。是否有我可以在某处更改的最大文件上传大小。我正在从我的本地桌面上传大约 100kb 的图片。帮助!

回答by Steve Robbins

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

如果你在调试器中检查 XHR 响应,你会看到这个 {"error":"File was not uploaded.","errorcode":666}

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

这个错误来自Varien_File_Uploader::__construct()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->_filearray based on the key image, in this case.

这是从 Varien 类扩展而来的,因此在这种情况下,Varien_File_Uploader::_setUploadFileId($fileId)$this->_file基于 key构造数组image

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

那么现在的问题是为什么是$_FILES['image']['tmp_name']空的?

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

'error'通过临时将异常更改为

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_diris set to and make sure it's writable.

我得到了7,这Failed to write file to disk.意味着这是一个权限问题。做一个phpinfo()检查你upload_tmp_dir的设置位置并确保它是可写的。

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

就我而言,我的/tmp目录中的文件空间不足。

回答by Alan Storm

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.

您报告的确切异常/错误消息并未以字符串形式显示在 Magento 的源代码中,因此我不能 100% 确定我在这里为您指明了正确的方向。

That said, most uploads in magento are handled by the savemethod on an instantiated object of the Varien_File_Uploaderclass.

也就是说,magento 中的大多数上传都是由类save的实例化对象上的方法处理的Varien_File_Uploader

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)

  1. 这是被调用的那个并且失败了

  2. 弄清楚为什么它可能会返回 false(即,不上传文件)

回答by zachwood

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.

不久前我在添加图像时遇到了一些问题,结果是 Flash 图像上传器是罪魁祸首。我找到了它是什么 swf 文件,并用我下载的较新版本的 Magento 替换了它。

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

如果这没有帮助,这里有一个模块可以让您在没有 Flash 上传器的情况下上传图像。您至少可以确保它不是闪存问题。

回答by Hendy Irawan

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

就我而言,uploader.swf 甚至不联系服务器并返回上传 I/O 错误或 SSL 错误。

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.

我尝试使用 Charles Proxy 并且“它有效”!!即当使用代理时,uploader.swf 现在可以工作了。没有代理就不行。

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

在我看来,问题完全出在 SWF 上传器中,根本不在服务器上。

回答by albatros88

verify your vhost include :

验证您的虚拟主机包括:

php_admin_value home_dir xxxxxxxxxxxxxx

So the upload_tmp_dirmust be included in this root directory otherwise magentofunctions cant catch tmp_file

所以upload_tmp_dir必须包含在这个根目录中,否则magento函数无法捕获tmp_file

eg: your configuration vhost include home_dir /home/someone

例如:您的配置虚拟主机包括 home_dir /home/someone

and php.iniwrite upload files in /tmp

php.ini在 /tmp 中写入上传文件

/tmpis not in dir home_dirand construct class function use file_exists()php cant read /tmp/

/tmp不在目录中home_dir并构造类函数使用file_exists()php无法读取/tmp/

you must create /home/someone/tmp

你必须创建 /home/someone/tmp

and include in vhostconfiguration

并包含在vhost配置中

php_admin_valueupload_tmp_dir/home/someone/tmp

php_admin_valueupload_tmp_dir/home/someone/tmp

apache2 reload

apache2 重新加载

hi

你好

回答by AbdulBasit

 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>";
            }
        }

回答by Attila Naghi

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

我也会检查upload_max_filesizewith phpinfo()。对我来说,它被设置为2M并且我的文件有3M,并更改它,解决了我的错误问题File was not uploaded

回答by Sanjaysinh Rajput

  • 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.
  • 更换浏览器、电脑、清除本地缓存和cookies等。
  • 检查 /media/ 文件夹的权限(试过 777 和 755)。
  • 如 Magento 根目录中的 .htaccess 文件所述,阅读并实施对 GoDaddy 服务器的 .htaccess 的更改
  • 禁用商店视图,刷新缓存,从 Magento 注销,从 /var/cache/ 删除缓存,然后重新启用商店视图,再次清除缓存
  • 上传并调用了早期版本的 Prototype.js
  • 当我读到 Prototype 和 jQuery 可能发生冲突时上传了 jQuery 的本地版本,这是建议的解决方案
  • 检查我的服务器的 PHP 版本中是否安装了 GD 扩展。