Upload video to YouTube using PHP

YouTube is one of the most popular for video upload and video-sharing. If your web application has video upload functionality, then upload video to YouTube would be the best idea. Once the upload is completed, display video at web application by the returned YouTube video ID. This will reduce the server space and provide the simple way for playing video.

In this tutorial we will discuss how can you upload video to YouTube from your web application using PHP. We have developed PHP scripts for upload video to YouTube using PHP OAuth library. You can view the live demo of the script from the Demo link and also can download the full Upload video on YouTube using PHP API project from the Download link.

Please follow the steps for configuring Upload video on YouTube using PHP project.

This YouTube video upload has two main folders named src/, includes/ and some PHP files. The folders and files structure are given below:

  • src/
    • Google API, Google Client, Google Oauth and other libraries
  • includes/
    • DB.php
  • config.php
  • index.php
  • youtube_upload.php
  • logout.php

Google Project Creation:

 Go to the Google Developers Console – https://console.developers.google.com/
 Select an existing project, or create a new project by clicking Create Project:

  • In the Project name field, type in a name for your new project.
  • In the Project ID field, the console has created project ID. Optionally you can type in a project ID for your project. But project ID must be unique world-wide.
  • Click on the Create button and the project to be created within some seconds. Once the project is created successfully, the project name would be appearing at the top of the left sidebar.

 In the left sidebar, select APIs under the APIs & auth section. A list of Google APIs appears.

 Find the Google+ API service and set its status to Enable.
 Find the YouTube Data API service and set its status to Enable.

 In the sidebar, select Credentials under the APIs & auth section.

 In the OAuth section of the page, select Create New Client ID.

  • Create Client ID dialog box would be appearing for choosing application type.
  • In the Application type section of the dialog, select Web application and click on the Configure consent screen button.
  • Choose Email address, enter Product name and save the form.
  • In the Authorized JavaScript origins field, enter your app origin. If you want to allow your app to run on different protocols, domains, or subdomains, then you can enter multiple origins.
  • In the Authorized redirect URI field, enter the redirect URL.
  • Click on Create Client ID.

 Now you can see the Client ID for web application section. Note the Client ID and Client secret that will need to use to access the APIs.


Database table creation:

To store the details of video, you need to create a database and a table. At first create a database like codexworld. After that copy the below SQL query and run the SQL query on the database.

CREATE TABLE `videos` (
    `video_id` int(11) NOT NULL AUTO_INCREMENT,
    `video_title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `video_description` text COLLATE utf8_unicode_ci NOT NULL,
    `video_tags` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `video_path` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `youtube_video_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `status` tinyint(1) NOT NULL DEFAULT '1',
    PRIMARY KEY (`video_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Project Configuration:

In this project you just have to modify only the following two files.
 Database configuration (includes/DB.php):
 DB.php file have DB class with some functions for handle the database works. Into the __construct() function we have configured the database and connect with the database. You need only to change the $dbHost, $dbUsername, $dbPassword and $dbName variables value with your MySQL server details.

function __construct(){
// Database configuration
$dbHost 'localhost';
$dbUsername 'root';
$dbPassword '';
$dbName 'codexworld';
// Connect database
$conn = new mysqli($dbHost$dbUsername$dbPassword$dbName);
"Failed to connect with MySQL: " $conn->connect_error);
$this->db $conn;

 Google API configuration (config.php): Open the config.php file and modify the $oauthClientID, $oauthClientSecret, $baseUri and $redirectUri variables value.
Note that: You can find the $clientId and $clientSecret at the Google project page.(Go to the https://console.developers.google.com/ => click on your project => click on the “APIs & auth” link from the left menu panel => click on the Credentials link from under the APIs & auth menu => copy the Client ID and Client Secret)

// OAUTH Configuration
$oauthClientID 'Insert Client ID';
$oauthClientSecret 'Insert Client Secret';
$baseUri 'http://localhost/upload-video-to-youtube-using-php/';
$redirectUri 'http://localhost/upload-video-to-youtube-using-php/youtube_upload.php';
// Include google client libraries
require_once 'src/Google/autoload.php'
$client = new Google_Client();
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

 If you have any query about this tutorial and scripts, feel free to comment here.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You


  1. Rahul Said...
  2. Kufre Said...
  3. Darshan Kini Said...
  4. Pritam Saha Said...
    • CodexWorld Said...
  5. Sachin Sharma Said...
  6. Ken Said...
  7. Jos Said...
    • CodexWorld Said...
  8. Hitesh Said...
  9. Cingcow Said...
  10. Arpit Kumar Pal Said...
  11. Rajdeep Singh Said...
  12. Developer Said...
  13. Adnane Said...
  14. User Said...
  15. Ahmed Said...
  16. Lalit Said...
  17. Randhir Said...
  18. Michoul Said...
  19. Developer Said...

Leave a reply