Multiple File Upload with Angular 9 FormData and PHP by Example

Multiple File Upload with Angular 9 FormData and PHP by Example

Angular PHP Upload FormData

In this tutorial, we'll learn by example how to implement Angular 9 multiple file upload with FormData and PHP. We'll also see an example of Angular Reactive forms for uploading multiple files.

Multiple File Uploading with Angular 9, FormData and PHP

Uploading multiple files in Angular 9 and FormData is easy.

In this example, we'll show you how to create a form for sending multiple files in your Angular 9 application.

We'll be using the reactive form/model based approach.

For the server, we'll use a simple REST API for receiving the upload files and store them in a folder using php:


 header("Access-Control-Allow-Origin: *");
 header("Access-Control-Allow-Methods: PUT, GET, POST");
 header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

 $uploadFolder =  "upload/";
 $files = $_FILES["file"]["name"];
 for  ($i =  0; $i < count($files); $i++)  {
    $ext =  end(explode(".", $filename));
    $original = pathinfo($filename, PATHINFO_FILENAME);
    $fileurl = $original .  "-"  . date("YmdHis")  .  "."  . $ext;
    move_uploaded_file($_FILES["file"]["tmp_name"][$i], $uploadFolder . $fileurl);

We'll implement a simple reactive form using formGroup.

We'll listen for the input onchange event, and add the selected file to a list. Next, after a click on the submit button of the form. we'll send an Http post request using Angular 9 HttpClient to the php server for uploading the file(s).

Step 1: Create an Angular 9 Project

Open your command-line interface and run the following command to initialize a new Angular 9 project:

$ ng new angular-9-upload-file-demo

Choose No for routing, and CSS for stylesheets format.

Step 2: Import Angular HttpClientModule, FormsModule and ReactiveFormsModule

Next, we need to import HttpClientModule, FormsModule and ReactiveFormsModule in the src/app/app.module.ts file:

import  {  BrowserModule  }  from  '@angular/platform-browser';
import  {  NgModule  }  from  '@angular/core';
import  {  HttpClientModule  }  from  '@angular/common/http';
import  {  FormsModule,  ReactiveFormsModule  }  from  '@angular/forms';
import  {  AppComponent  }  from  './app.component';

 declarations:  [
 imports:  [
 providers:  [],
 bootstrap:  [AppComponent]
export  class  AppModule  {  }

Step 3: Create a Reactive Form

Let's now add a reactive form with an input element with the file type.

Open the src/app/app.component.html

<h1>Angular 9 Multiple File Upload Example</h1>

<form [formGroup]="uploadForm" (ngSubmit)="submitForm()">

  <div  class="form-group">

  <label  for="name">Name</label>






  <div *ngIf=" &&">

  <div *ngIf="">Filename is required</div>



  <div  class="form-group">

  <label  for="file">File</label>








  <div *ngIf="f.file.touched && f.file.invalid">
  <div *ngIf="f.file.errors.required">Choose a file</div>
  <button type="submit">Upload</button>


Step 4: Implement the Angular Reactive Form Using formGroup and formControl

Now, let's implment the reactive form for uploading the selected files using formGroupand formControl.

Later, we'll be running the php script from http://localhost:8000/upload.php for receiving the upload files and save them in the server.

Open the src/app/app.component.ts file and update it as follows:

import  {  Component  }  from  '@angular/core';
import  {  HttpClient  }  from  '@angular/common/http';
import  {  FormGroup,  FormControl,  Validators}  from  '@angular/forms';

 selector:  'app-root',
 templateUrl:  './app.component.html',
 styleUrls:  ['./app.component.css']
export  class  AppComponent  {
 URL= "http://localhost:8000/upload.php";
 files:string  []  =  [];
 uploadForm =  new  FormGroup({
    name:  new  FormControl('',  [Validators.required,  Validators.minLength(3)]),
    file:  new  FormControl('',  [Validators.required])

 constructor(private httpClient:  HttpClient)  {  }

  get f(){
     return  this.uploadForm.controls;

 onFileChange(event)  {
    for  (var i =  0; i <; i++)  {  


  const formData =  new  FormData();
  for  (var i =  0; i <  this.myFiles.length; i++)  {  
      formData.append("file[]",  this.myFiles[i]);
  }, formData).subscribe(res =>  {
      alert('Files uploaded Successfully!');

Finally, start the Angular 9 development server using the following command:

ng serve

Next, start the PHP file uploading server using the following command:

php -S localhost:8000

  • Author: Techiediaries Team

Angular 9 Components: Input and Output
Angular 13 selectors
Picture-in-Picture with JavaScript and Angular 10
Jasmine Unit Testing for Angular 12
Angular 9 Tutorial By Example: REST CRUD APIs & HTTP GET Requests with HttpClient
Angular 10/9 Elements Tutorial by Example: Building Web Components
Angular 10/9 Router Tutorial: Learn Routing & Navigation by Example
Angular 10/9 Router CanActivate Guards and UrlTree Parsed Routes
Angular 10/9 JWT Authentication Tutorial with Example
Style Angular 10/9 Components with CSS and ngStyle/ngClass Directives
Upload Images In TypeScript/Node & Angular 9/Ionic 5: Working with Imports, Decorators, Async/Await and FormData
Angular 9/Ionic 5 Chat App: Unsubscribe from RxJS Subjects, OnDestroy/OnInit and ChangeDetectorRef
Adding UI Guards, Auto-Scrolling, Auth State, Typing Indicators and File Attachments with FileReader to your Angular 9/Ionic 5 Chat App
Private Chat Rooms in Angular 9/Ionic 5: Working with TypeScript Strings, Arrays, Promises, and RxJS Behavior/Replay Subjects
Building a Chat App with TypeScript/Node.js, Ionic 5/Angular 9 & PubNub/Chatkit
Chat Read Cursors with Angular 9/Ionic 5 Chat App: Working with TypeScript Variables/Methods & Textarea Keydown/Focusin Events
Add JWT REST API Authentication to Your Node.js/TypeScript Backend with TypeORM and SQLite3 Database
Building Chat App Frontend UI with JWT Auth Using Ionic 5/Angular 9
Install Angular 10 CLI with NPM and Create a New Example App with Routing
Styling An Angular 10 Example App with Bootstrap 4 Navbar, Jumbotron, Tables, Forms and Cards
Integrate Bootstrap 4/jQuery with Angular 10 and Styling the UI With Navbar and Table CSS Classes
Angular 10/9 Tutorial and Example: Build your First Angular App
Angular 9/8 ngIf Tutorial & Example
Angular 10 New Features
Create New Angular 9 Workspace and Application: Using Build and Serve
Angular 10 Release Date: Angular 10 Will Focus on Ivy Artifacts and Libraries Support
HTML5 Download Attribute with TypeScript and Angular 9
Angular 9.1+ Local Direction Query API: getLocaleDirection Example
Angular 9.1 displayBlock CLI Component Generator Option by Example
Angular 9 Basics Tutorial by Example
Angular 9/8 ngFor Directive: Render Arrays with ngFor by Example
Responsive Image Breakpoints Example with CDK's BreakpointObserver in Angular 9/8
Angular 9/8 DOM Queries: ViewChild and ViewChildren Example
The Angular 9/8 Router: Route Parameters with Snapshot and ParamMap by Example
Angular 9/8 Nested Routing and Child Routes by Example
Angular 9 Examples: 2 Ways To Display A Component (Selector & Router)
Angular 9/8 Tutorial: Http POST to Node/Express.js Example
Angular 9/8 Feature and Root Modules by Example
Angular 9/8 with PHP: Consuming a RESTful CRUD API with HttpClient and Forms
Angular 9/8 with PHP and MySQL Database: REST CRUD Example & Tutorial
Unit Testing Angular 9/8 Apps Tutorial with Jasmine & Karma by Example
Angular 9 Web Components: Custom Elements & Shadow DOM
Angular 9 Renderer2 with Directives Tutorial by Example
Build Progressive Web Apps (PWA) with Angular 9/8 Tutorial and Example
Angular 9 Internationalization/Localization with ngx-translate Tutorial and Example
Create Angular 9 Calendar with ngx-bootstrap datepicker Example and Tutorial
Multiple File Upload with Angular 9 FormData and PHP by Example
Angular 9/8 Reactive Forms with Validation Tutorial by Example
Angular 9/8 Template Forms Tutorial: Example Authentication Form (ngModel/ngForm/ngSubmit)
Angular 9/8 JAMStack By Example
Angular HttpClient v9/8 — Building a Service for Sending API Calls and Fetching Data
Upgrade Angular 8/7 to Angular 9 With Ng Update & Angular CLI v9
Build an Angular 9/8 CRUD Example & Tutorial
Styling An Angular 9/8/7 Example App with Bootstrap 4 Navbar, Jumbotron, Tables, Forms and Cards
Angular 9/8 CLI Commands: Install Angular CLI On Windows 10, Linux and macOS
Angular 8/7 Tutorial By Example: (REST API, HttpClient GET, Components, Services & ngFor)