Don't know if you will be able to do this in PHP the way you described. Let's say we have this HTML:
So now we have to make a PHP page that handles this ( See Using php to output an mp4 video ):
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_open — Opens file or URL Descriptionpublic streamWrapper::stream_open( Parameterspath Specifies the URL that was passed to the original function.
mode The mode used to open the file, as detailed for fopen().
options Holds additional flags set by the streams API. It can hold one or more of the following values OR'd together.
opened_path If the Return Values Returns Errors/ExceptionsEmits Notes
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. PHPOutput: Before running the program: php source folder After running the program: File downloaded after successful execution 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:
Example: This example illustrates the use of the PHP Curl to make HTTP requests in PHP, in order to download the file. PHP
Output: Before running the program: php source folder After running the program: 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. |