简体   繁体   English

使用Varien_File_Uploader上传Magento文件失败-$ _FILES ['tmp_name']文件不存在

[英]Magento file upload with Varien_File_Uploader fails - $_FILES['tmp_name'] file doesn't exist

I've included observer code in my custom module for 'customer_save_before' event. 我已经在我的自定义模块中为“ customer_save_before”事件添加了观察者代码。 It fires when customer submits his details in Magento front, account management area. 当客户在Magento Front(客户管理)区域中提交其详细信息时会触发。 It executes a script that should intercept $_FILES[...] array and use Varien_File_Uploader to save it in database. 它执行一个脚本,该脚本应拦截$ _FILES [...]数组,并使用Varien_File_Uploader将其保存在数据库中。

I added a new field to .../template/customer/form/edit.phtml 我在... / template / customer / form / edit.phtml中添加了一个新字段

<input type="file" name="logo" id="logo" title="<?php echo $this->__('Logo') ?>" class="input-file" />

This is my Observer.php code that extcutes on 'customer_save_before': 这是我在'customer_save_before'上执行的Observer.php代码:

class Walder_Logoupload_Model_Observer extends Mage_Core_Model_Abstract {

 public function customer_save_before($observer) {

    // Test code START

        echo "<pre>"; print_r($_FILES);

        $tempexists = file_exists($_FILES['logo']['tmp_name'])?'exists':'doesnt exist';
        echo "logo.tmp_name file: ".$tempexists;
        exit;

    // Test code END

        if(isset($_FILES['logo']['name'])) {
          try {
            $uploader = new Varien_File_Uploader('logo');
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            $uploader->setAllowRenameFiles(false);
            $uploader->setFilesDispersion(false);

            $path       = Mage::getBaseDir('media') . DS .'catalog'.DS.'customer'.DS.'logo';
            $newName    = time() . $_FILES['logo']['name'];
            $uploader->save($path, $newName);
            $customer->setLogo($newName);
          }catch(Exception $e) {
                echo "Exception: ".$e; exit;
          }
        }
 }
}

After file upload, with test code enabled I get an array that contains file info along with 'tmp_name' path and a message that temp file doesn't exist: 上传文件后,启用测试代码后,我得到一个包含文件信息以及'tmp_name'路径和临时文件不存在的消息的数组:

[logo] => Array
    (
        [name] => sample-logo.png
        [type] => image/png
        [tmp_name] => /home/www/mag59212/tmp/phpbCoax5
        [error] => 0
        [size] => 107564
    )

logo.tmp_name file: doesnt exist

With test code commented out I get this Exception error message: 注释掉测试代码后,我收到此异常错误消息:

exception 'Exception' with message 'File was not uploaded.' 
in /home/www/mag59212/html/magento-de/lib/Varien/File/Uploader.php:153

Which means the same, $_FILES['tmp_name'] file does not exist. 这意味着相同的$ _FILES ['tmp_name']文件不存在。 In consequence the file is not saved on the server. 因此,该文件未保存在服务器上。

When I run this Observer code in back end I get same messages for my test code(file doesnt exist) but the file gets uploaded anyway. 当我在后端运行此Observer代码时,我得到相同的测试代码消息(文件不存在),但无论如何该文件都会被上传。

How do I fix my front end code to upload this file? 如何修复前端代码以上传此文件?

I had a similar problem. 我有一个类似的问题。 Did you checked the system log? 您检查系统日志了吗?

MAGENTO FOLDER/var/log/system.log MAGENTO文件夹/var/log/system.log

For me it says: 对我来说,它说:

Warning: file_exists() [function.file-exists]: open_basedir restriction in effect. 警告:file_exists()[function.file-exists]:open_basedir限制有效。 File(/var/tmp/phpSJjFIv) is not within the allowed path(s): (...) /httpdocs/lib/Varien/File/Uploader.php on line 150 File(/ var / tmp / phpSJjFIv)不在允许的路径内:(...)/httpdocs/lib/Varien/File/Uploader.php行150

Look up the php settings in php.ini, or phpinfo(). 在php.ini或phpinfo()中查找php设置。 Search for "open_basedir" and "upload_tmp_dir". 搜索“ open_basedir”和“ upload_tmp_dir”。 If the open_basedir restriction is set, and the upload_tmp_dir is not in the allowed directories, it could fire up such an error. 如果设置了open_basedir限制,并且upload_tmp_dir不在允许的目录中,则可能会引发此类错误。

At my end the issue was on file: Mage/Customer/controllers/AccountController.php at code : 在我的末端,该问题已归档:Mage / Customer / controllers / AccountController.php,代码为:

 $customerForm->compactData($customerData);

in $customerData my file custom attribute is also receiving when compactData function called it removed the file. 在$ customerData中,当compactData函数调用它删除文件时,我的文件自定义属性也正在接收。 and it included due to the following line in attribute creation in setup file: 并且由于在安装文件中创建属性的以下行而将其包括在内:

$used_in_forms[]="customer_account_create";

when I removed the above line from the custom attribute setup line it resolve the issue. 当我从自定义属性设置行中删除上述行时,它解决了该问题。 hope will helpful to someone having similar issue, 希望对有类似问题的人有所帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM