Skip to content

Mukesh Chapagain Blog

  • PHP
    • PHP
    • Laravel
    • WordPress
    • Joomla
  • Magento
    • Magento 2
    • Magento Extension
  • Node.js
    • Node.js
    • Javascript
    • jQuery
  • Database
    • MySQL
    • MongoDB
  • Data Science
    • Machine Learning
    • Recommender System
    • Natural Language Processing (NLP)
    • Sentiment Analysis
    • Python
    • R
  • Categories
    • Blockchain
      • Hyperledger Composer
      • Hyperledger-Fabric
    • Other
      • Cryptography
      • Data Structures & Algorithms
      • Git
      • LaTeX
      • Linux
      • Ubuntu
      • Xubuntu
      • Google
      • Google AppScript
  • About
    • About
    • Contact
    • Privacy Policy

Home » Magento » Magento 2 » Magento 2: Customer Image/File Upload in Registration & Account Page

Magento 2: Customer Image/File Upload in Registration & Account Page

October 6, 2022March 26, 2019 by Mukesh Chapagain
Categories Magento, Magento 2 Tags account, customer, Magento, magento2, registration
FacebookTweetLinkedInPinPrintEmailShares

This article shows how you can upload file or image in the customer registration and in the customer account page in Magento 2.

For this, first, we need to add a new customer attribute. In this example, we will be adding a new image attribute for the customer so that we can upload the customer image.

I have written an article before about creating customer attribute. Here’s the link: Magento 2: Create Customer Attribute Programmatically [Also Update & Delete Customer Attribute]

Here, we will create a new customer attribute from our module’s install script. We assume our module’s name to be Chapagain_CustomerAttribute.

Table of Contents

Toggle
  • Add/Create Customer Attribute Using the Install Script
  • Add Image Upload Field in Customer Registration Page
  • Add Image Upload Field in Customer Account Page

Add/Create Customer Attribute Using the Install Script

Here, we create a customer attribute with the attribute code my_customer_image.

File: app/code/Chapagain/CustomerAttribute/Setup/InstallData.php


<?php

namespace Chapagain\CustomerAttribute\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var \Magento\Eav\Setup\EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param EavSetupFactory $eavSetupFactory
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        EavSetupFactory $eavSetupFactory,
        CustomerSetupFactory $customerSetupFactory
    ) 
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        /**
         * Create a file type attribute
         * For File or Image Upload
         */
        $attributeCode = 'my_customer_image';

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY, 
            $attributeCode, 
            [
                'type' => 'text',
                'label' => 'My Customer File/Image',
                'input' => 'file',
                'source' => '',
                'required' => false,
                'visible' => true,
                'position' => 200,
                'system' => false,
                'backend' => ''
            ]
        );
        
        // show the attribute in the following forms
        $attribute = $customerSetup
                        ->getEavConfig()
                        ->getAttribute(
                            \Magento\Customer\Model\Customer::ENTITY, 
                            $attributeCode
                        )
                        ->addData(
                            ['used_in_forms' => [
                                'adminhtml_customer',
                                'adminhtml_checkout',
                                'customer_account_create',
                                'customer_account_edit'
                            ]
                        ]);

        $attribute->save();

        $setup->endSetup();
    }
}

Add Image Upload Field in Customer Registration Page

For this, you need to create a new XML layout file and a new template file.

We define the block class and template file in the newly created XML file.

Here’s the XML file code:

File: app/code/Chapagain/CustomerAttribute/view/frontend/layout/customer_account_create.xml


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="additional_field_register" template="Chapagain_CustomerAttribute::customer/form/additionalfieldregister.phtml"/>
        </referenceContainer>
    </body>
</page>

Here’s the template file code where we add the image input field code:

File: app/code/Chapagain/CustomerAttribute/view/frontend/template/customer/form/additionalfieldregister.phtml


<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */
echo __('* Required Fields') ?>">
    <legend class="legend">
        <span>
        <?php 
            /* @escapeNotVerified */ 
            echo __('Additional Information') 
        ?>
        </span>
    </legend>
 
    
        <div class="field my_customer_image ">
            <label for="my_customer_image" class="label"><span><?php /* @escapeNotVerified */
                    echo __('Logo Image') ?></span></label>
            <div class="control">
                <input type="file" name="my_customer_image" id="my_customer_image" title="<?php /* @escapeNotVerified */
                echo __('Logo Image') ?>" class="input-text" data-validate="{required:false}">
            </div>
        </div>
    
</fieldset>

Note: The image/file uploaded using the above file field will be stored inside the pub/media/customer folder.

Add Image Upload Field in Customer Account Page

Here’s the XML file code:

File: app/code/Chapagain/CustomerAttribute/view/frontend/layout/customer_account_edit.xml


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Chapagain\CustomerAttribute\Block\Customer\Account" name="additional_field_account" template="Chapagain_CustomerAttribute::customer/form/additionalfieldaccount.phtml"/>
        </referenceContainer>
    </body>
</page>

As you can see in the above XML code, we also have defined a Block class. So, we need to create one:

File: app/code/Chapagain/CustomerAttribute/Block/Customer/Account.php


<?php
namespace Chapagain\CustomerAttribute\Block\Customer;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\UrlInterface;
use Magento\Customer\Model\SessionFactory;

class Account extends \Magento\Framework\View\Element\Template
{        
    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
    * @var \Magento\Store\Model\StoreManagerInterface $storeManager
    */
    protected $storeManager;

    /**
     * @var \Magento\Customer\Model\Customer 
     */
    protected $customerModel;

    public function __construct(
        Context $context,
        UrlInterface $urlBuilder,
        SessionFactory $customerSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\Customer $customerModel,
        array $data = []
    )
    {        
        $this->urlBuilder            = $urlBuilder;
        $this->customerSession       = $customerSession->create();
        $this->storeManager          = $storeManager;
        $this->customerModel         = $customerModel;

        parent::__construct($context, $data);

        $collection = $this->getContracts();
        $this->setCollection($collection);
    }

    public function getBaseUrl()
    {
        return $this->storeManager->getStore()->getBaseUrl();
    }

    public function getMediaUrl()
    {
        return $this->getBaseUrl() . 'pub/media/';
    }

    public function getCustomerLogoUrl($logoPath)
    {
        return $this->getMediaUrl() . 'customer' . $logoPath;
    }

    public function getLogoUrl()
    {
        $customerData = $this->customerModel->load($this->customerSession->getId());
        $logo = $customerData->getData('my_customer_image');
        if (!empty($logo)) {
            return $this->helper->getCustomerLogoUrl($logo);
        }
        return false;
    }
}
?>

Here’s the template file code where we add the image input field code:

File: app/code/Chapagain/CustomerAttribute/view/frontend/template/customer/form/additionalfieldaccount.phtml


<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */
echo __('* Required Fields') ?>">
    <legend class="legend">
        <span>
        <?php 
            /* @escapeNotVerified */ 
            echo __('Logo') 
        ?>
        </span>
    </legend>
 
    
        <?php if ($logo = $block->getLogoUrl()): ?>
            <img src="<?php echo $logo ?>" alt="logo" />
        <?php endif; ?>
    

     
        <div class="field my_customer_image ">
            <label for="my_customer_image" class="label"><span><?php /* @escapeNotVerified */
                    echo __('Logo Image') ?></span></label>
            <div class="control">
                <input type="file" name="my_customer_image" id="my_customer_image" title="<?php /* @escapeNotVerified */
                echo __('Logo Image') ?>" class="input-text" data-validate="{required:false}">
            </div>
        </div>
    
</fieldset>

Hope this helps. Thanks.

Related posts:

  1. Magento 2: Create, Edit, Delete Customer Attribute Programmatically
  2. Magento 2: Add Custom Link/Tab & Page to Customer Account Sidebar
  3. Magento: Show Billing-Shipping Address in Customer Signup-Registration page
  4. Magento: Add new tab to Customer Account Section
Categories Magento, Magento 2 Tags account, customer, Magento, magento2, registration
Magento 2: Setup Multiple Stores and Websites
Magento 2: Create/Add Product Programmatically [Simple & Virtual Product with Image, Category & Custom Option]

About

Mukesh Chapagain Hi, I’m Mukesh Chapagain — a web developer, programmer, and tech enthusiast. Whether you're a beginner or an experienced developer, you’ll find tips, tutorials, and insights to help you navigate the ever-evolving world of technology. Happy coding! 🚀 about...

         

Subscribe via Email



Categories

Most Viewed

  • Very Simple Add, Edit, Delete, View (CRUD) in PHP & MySQL [Beginner Tutorial] - 435,128 views
  • How to Calculate Inverter & Battery Backup Time? - 430,453 views
  • LaTeX: Generate dummy text (lorem ipsum) in your document - 238,158 views
  • GPG: Remove keys from your public keyring? - 203,599 views
  • Magento: How to get attribute name and value? - 189,657 views

Recent Posts

  • Magento: The store that was requested wasn’t found. Verify the store and try again.
  • Magento2: Check Services version on Adobe Cloud Server
  • Magento 2 API: Add Products to Cart & Checkout Place Order
  • Magento 2 API: Create New Customer
  • Magento 2 API: Get Categories

Recent Posts

  • Magento: The store that was requested wasn’t found. Verify the store and try again.
  • Magento2: Check Services version on Adobe Cloud Server
  • Magento 2 API: Add Products to Cart & Checkout Place Order
  • Magento 2 API: Create New Customer
  • Magento 2 API: Get Categories

Most Viewed

  • Very Simple Add, Edit, Delete, View (CRUD) in PHP & MySQL [Beginner Tutorial] - 435,128 views
  • How to Calculate Inverter & Battery Backup Time? - 430,453 views
  • LaTeX: Generate dummy text (lorem ipsum) in your document - 238,158 views
  • GPG: Remove keys from your public keyring? - 203,599 views
  • Magento: How to get attribute name and value? - 189,657 views

Tag Cloud

admin Adobe Commerce api array attribute block category checkout CLI command line crud currency customer Database error extension git Google grid HTML image Javascript Joomla jQuery latex Linux login Magento magento2 magento 2 module MySQL natural language processing NLP nltk nodejs order PHP product python shopping cart template Ubuntu url Wordpress
© 2026 Mukesh Chapagain Blog
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}