简体   繁体   中英

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. It fires when customer submits his details in Magento front, account management area. It executes a script that should intercept $_FILES[...] array and use Varien_File_Uploader to save it in database.

I added a new field to .../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':

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:

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

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

For me it says:

Warning: file_exists() [function.file-exists]: open_basedir restriction in effect. File(/var/tmp/phpSJjFIv) is not within the allowed path(s): (...) /httpdocs/lib/Varien/File/Uploader.php on line 150

Look up the php settings in php.ini, or phpinfo(). Search for "open_basedir" and "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.

At my end the issue was on file: Mage/Customer/controllers/AccountController.php at code :

 $customerForm->compactData($customerData);

in $customerData my file custom attribute is also receiving when compactData function called it removed the file. 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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