Php stream file from url

Don't know if you will be able to do this in PHP the way you described.

Let's say we have this HTML:

<video width="320" height="240" controls>
    <source src="playmymovie.php?url=http://domain/video-path/video.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

So now we have to make a PHP page that handles this ( See Using php to output an mp4 video ):

<?php
$vid_url = isset($_GET['url'])?$_GET['url']:"";
if(empty($vid_url)){
    // trigger 404
    header("HTTP/1.0 404 Not Found");
} else {
    // Get Video

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $vid_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $out = curl_exec($ch);
    curl_close($ch);

    // Set header for mp4
    header('Content-type: video/mp4');
    header('Content-type: video/mpeg');
    header('Content-disposition: inline');
    header("Content-Transfer-Encoding:­ binary");
    header("Content-Length: ".filesize($out));

    // Pass video data
    echo $out;
}
exit();
?>

If it were me, I would test the URL first, make sure you get a 200 status, before trying to pass it back out. You could also trigger the correct status so the browser knows whats going on and can alert the user (or you).

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

streamWrapper::stream_openOpens file or URL

Description

public streamWrapper::stream_open(
    string $path,
    string $mode,
    int $options,
    ?string &$opened_path
): bool

Parameters

path

Specifies the URL that was passed to the original function.

Note:

The URL can be broken apart with parse_url(). Note that only URLs delimited by :// are supported. : and :/ while technically valid URLs, are not.

mode

The mode used to open the file, as detailed for fopen().

Note:

Remember to check if the mode is valid for the path requested.

options

Holds additional flags set by the streams API. It can hold one or more of the following values OR'd together.

FlagDescription
STREAM_USE_PATH If path is relative, search for the resource using the include_path.
STREAM_REPORT_ERRORS If this flag is set, you are responsible for raising errors using trigger_error() during opening of the stream. If this flag is not set, you should not raise any errors.
opened_path

If the path is opened successfully, and STREAM_USE_PATH is set in options, opened_path should be set to the full path of the file/resource that was actually opened.

Return Values

Returns true on success or false on failure.

Errors/Exceptions

Emits E_WARNING if call to this method fails (i.e. not implemented).

Notes

Note:

The streamWrapper::$context property is updated if a valid context is passed to the caller function.

In this article, we will see how to download & save the file from the URL in PHP, & will also understand the different ways to implement it through the examples. There are many approaches to download a file from a URL, some of them are discussed below:

Using file_get_contents() function: The file_get_contents() function is used to read a file into a string. This function uses memory mapping techniques that are supported by the server and thus enhances the performance making it a preferred way of reading the contents of a file.

Syntax:

file_get_contents($path, $include_path, $context, 
                  $start, $max_length)

Example 1: This example illustrates the use of file_get_contents() function to read the file into a string.

PHP

Output:

Before running the program: 

Php stream file from url

php source folder

After running the program:

Php stream file from url

File downloaded after successful execution

Php stream file from url

Downloaded image file

Using PHP Curl:  The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’. The cURL project has two products libcurl and curl.

Steps to download the file:  

  • Initialize a file URL to the variable.
  • Create cURL session.
  • Declare a variable and store the directory name where the downloaded file will save.
  • Use the basename() function to return the file basename if the file path is provided as a parameter.
  • Save the file to the given location.
  • Open the saved file location in write string mode.
  • Set the option for cURL transfer.
  • Perform cURL session and close cURL session and free all resources.
  • Close the file.

Example: This example illustrates the use of the PHP Curl to make HTTP requests in PHP, in order to download the file.

PHP

<?php

  $url

  $ch = curl_init($url);

  $dir = './';

  $file_name = basename($url);

  $save_file_loc = $dir . $file_name;

  $fp = fopen($save_file_loc, 'wb');

  curl_setopt($ch, CURLOPT_FILE, $fp);

  curl_setopt($ch, CURLOPT_HEADER, 0);

  curl_exec($ch);

  curl_close($ch);

  fclose($fp);

?>

Output:

Before running the program:

Php stream file from url

php source folder

After running the program:

Php stream file from url

Downloaded image file

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.