Tutorials · 7 min read

Upload a video with PHP

How to upload a video file in PHP

In this step by step guide, learn how to upload a video using PHP while making sure that users have a good experience as they upload and stream these videos.

Artem Matinian

November 28, 2023

When considering building your project or website, you went through several languages and eventually settled for PHP. It’s a great language, easy to pick up and it can help you build your own project within minutes.

 

However, with all the great things that come with PHP, there are also some challenges. For example, how do you upload videos with PHP, while also making sure that users have a good experience while uploading and streaming these videos?

 

You are probably already aware of some of these challenges, otherwise, you wouldn’t be reading this article. 😄

How to upload a video: A step-by-step guide

For the sake of this example, let’s stick to the basics and build a very simple video uploader on an existing PHP server. The workflow is quite simple:

 

  1. Your PHP server shows the user a file selector.
  2. The file selector lets the user pick a file from their device.
  3. The selected file is delivered in a multi-part form to the PHP server.
  4. The PHP server then uploads the video to api.video for transcoding, hosting, and delivery.

 

Now, let’s start with creating a simple PHP server. You can skip the Preparation if you already have PHP implementation set up.

Preparation

1. Sign up to api.video for free

Let’s create a free account with api.video. We’ll handle the video transcoding, hosting, and delivery for your viewers.

2. Install PHP

You can skip this step if you have PHP already installed. If not, you can find instructions in the PHP official installation guide.  

Alternatively, you can use MAMP to create a local server environment. Visit the official MAMP website for more information.

3. Install Composer

In order to get all the respective libraries for the use case we are building here, you’ll need a package manager. Let’s install Composer, a simple package manager for PHP. Check out the instructions on how to download and install Composer here.

4. Create a PHP server folder

  1. Let’s a new folder for the app. Choose any name for the project.
  2. Create a new file called server.php inside that folder.

 

Now that you’re all set, let’s begin with the process.

Create the video uploader frontend

We’ll start by setting up the PHP server and the multipart form.

 

  1. The server will receive a request: we will check that the request type is GET.
  2. We will also check that and that the URL that the user is hitting is /. This means that the user landed on the main page.
  3. The multipart type form is necessary if the user uploads a file through the form. Give the form a name and an id. We’ll use it later on.

 

Copy this script into your server.php file:

php

Now let’s do a quick test run!

 

  1. Open your cmd or terminal
  2. Navigate to the PHP folder you've stored the project in

bash

  1. Run the PHP server (this command will run the file server.php on your local machine on port 8000)

bash

  1. Navigate to localhost:8000 on your browser
localhost

You should get the following screen:

Video uploader

If this is the first time you are working with PHP, then congratulations - you’ve just created your first PHP website!

Create the backend

Now that the frontend part is done, let’s set up the backend. The backend’s job is to:

 

  1. Receive the videos from the frontend
  2. Send the videos over to api.video in order to make sure your users have the best experience while uploading and streaming.

Configure file uploads in PHP

Let’s enable file uploading, and configure the size of the files your users can upload. To configure this, we need to find the php.ini in your implementation.

 

  1. In your terminal, type:

bash

This should give you the following output:

bash

Navigate to the php.ini file, and change or add these following values:

php

You can choose smaller or larger values than 150M. Now let’s save it.

Receive the video files

Before the file is sent to api.video, it has to be stored locally. This is only temporary and you can delete the file after your server uploads it to api.video.

This is the workflow we’ll follow:

 

  1. First, we create a new “endpoint” that accepts POST requests to an /upload endpoint. Remember our multipart form? It is actually already set up to submit the file to /upload. Multipart form are set to POST by default. So, we will create an if statement to handle that.
  2. Once a POST request will hit this endpoint, it will pass in the file. We pick it up and save it in a /uploads/ directory in the PHP server folder. Let’s create the /uploads folder inside the PHP project folder:

bash

Now edit the server.php file and add this code we just described:

php

With this code, we take the file name and store it in the /uploads folder with the move_uploaded_file function.

 

Awesome! Now we have a cool server that can ask the user for a file. Let’s run the server again to test it. Start the PHP server again:

bash

Navigate to localhost:8000 and this time, select a file to upload and click on Upload Video. After the file is uploaded, you should see it in the /uploads folder in your PHP server project.

Upload folder PHP server

Upload the videos to api.video

You’re more than half-way done, great job!

 

Now, you only need to add a small thing to all the mix. After uploading the video to your PHP server, you can just give your users public access to this file, and let them play it, right?

 

Not quite. There’s a lot of science behind delivering a video. While it can be delivered directly from your PHP server, your users may see issues with the playback: long loading times, and even issues with loading the video at all. It comes down to transcoding the video to different qualities, hosting the video on a scalable environment and having a good enough CDN that will deliver the video fast. You also need a capable player that will have all the options that your users need, without having to pour vast amount of money and resources into expensive services.

 

With around ten simple lines of code, you can make sure your users have the best experience while watching and uploading their videos.

Add api.video to your code

Now that you have an api.video account, grab your API key from the dashboard and paste it in as the value of $apiKey in the server.php:

php

Notice that we are also initializing the api.video client library. So, we need to run the Composer in order to get these libraries inside the server’s main folder.

 

  1. Let’s make sure that Composer is installed.
  2. Open the terminal again and run these commands:

bash

  1. After the packages are installed, run the PHP server again:

bash

Deliver the videos

Let’s try upload a new video in your localhost environment.

 

Since we implemented api.video in the code, uploading a video will give you a response with different parameters, including the assets. The assets are actually the video you just uploaded in different streaming formats (HLS and mp4). You’ll notice that you also have an iframe parameter, which is your video embedded in an iframe. Let’s display the video as soon as it is ready. We’ll add a bit more code to retrieve the status of the video, so once it’s ready, we can display it in the iframe.

 

Let’s add a status request to our code:

php

We’ve added a while loop that checks the video status. If the video is playable (which means that api.video finished transcoding it), we can take the iframe asset, embed it, and display it to the users. This part is handled in echo $response->getAssets()->getIframe().

 

Now save your code, upload a new video via localhost, and enjoy the show! 😊

 

Start using api.video and see how this works

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.