Download file through a server to user - php

There is a file located on a server. Lets call it "Movie".
My site's users need to downoad this movie, but the movie can only be downloaded by my website's IP.
Is there a way to download Movie to my server and from there to the user?
I can do that by "put_content" but I need the client to download the file WHILE my server downloads it.
Thanks!

You can make use of the standard wrappers PHP offers­Docs and combine that with stream_copy_to_stream­Docs:
Example / Demo:
<?php
$url = 'http://example.com/some.url';
$src = fopen($url, 'r');
$dest = fopen('php://output', 'w');
$bytesCopied = stream_copy_to_stream($src, $dest);
Related (couldn't find a better duplicate so far):
Remotely download a file from an external link to my server - download stops prematurely

Take a look at the readfile function.
http://php.net/manual/en/function.readfile.php

Related

I want to download a proxylist with curl in php

I want to download a proxy list with this api:
https://api.proxyscrape.com?request=getproxies&proxytype=http&timeout=5000&country=US&anonymity=elite&ssl=yes
how can I do it in php with curl???
When you open this url, It will automatically download a text file with proxies.
I want to download and place it in the current directory.
The easiest way would be using file_get_contents() function built into PHP.
So to download & save the file you would do something like this:
$proxies = fopen("proxies.txt", "w");
fwrite($proxies, file_get_contents('https://api.proxyscrape.com?request=getproxies&proxytype=http&timeout=5000&country=US&anonymity=elite&ssl=yes'));
fclose($proxies);

Download .mp4 from URL using Laravel

How to download .m4v video from given URL with Laravel? I have a URL for example rtmp://123456.r.cdnsun.net/_definst_/mp4:123456/h264/123456.m4v ... Can you recommend me a package for Laravel which could make the process easier?
It depends on what exactly you're trying to achieve. For example, if you want to just copy file from a remote server, you could use copy():
$remoteFile = 'rtmp://123456.r.cdnsun.net/_definst_/mp4:123456/h264/123456.m4v';
$localFile = storage_path().'movies';
copy($remoteFile, $localFile);
You can use file_put_contents to download video from URL
$video_name="video_name.mp4";
$video_path=public_path('videos').'/'.$video_name; // path where you want to store video
file_put_contents($video_path, fopen($file, 'r'));

Get screenshot from url2png and save to FTP

I am using url2png to get screenshots from pages. However, instead of asking the image every time, I want to save it onto an external server via FTP.
My first approach was to use:
$image = fopen($src,"r");
And then an ftp_fput. But as url2png may take about 5 secs to get the screenshot, the ftp_fput uploads an empty file.
Do I need to save the file locally first? or is there a workaround?
Thanks!
Found a solution using this question: Using file_get_contents and ftp_put
$fp = fopen('php://temp', 'r+');
fputs($fp, file_get_contents($src));
rewind($fp);

Download and rename a file via url with PHP

I have this URL
www1.intranet.com/reportingtool.asp?settings=var&export = ok
There I can download a report. The file-name of the report includes a timestamp. e.g. 123981098298.xls and varies everytime I download it.
I want to have a script with this functions:
<?php
//Download the File
//rename it to **report.xls**
//save it to a specified place
?>
I don't have any idea after searching stackoverflow and googling on this topic :(
Is this generally possible?
The simplest scenario
You can download the report with file_get_contents:
$report = file_get_contents('http://www1.intranet.com/reportingtool.asp?...');
And save it locally (on the machine where PHP runs) with file_put_contents:
file_put_contents('/some/path/report.xls', $report);
More options
If downloading requires control over the HTTP request (e.g. because you need to use cookies or HTTP authentication) then it has to be done through cURL which enables full customization of the request.
If the report is large in size then it could be directly streamed to the destination instead of doing read/store/write in three steps (for example, using fopen/fread/fwrite).
This may not work depending on your security settings, but it's a simple example:
<?php
$file = file_get_contents('http://www1.intranet.com/reportingtool.asp?settings=var&export=ok');
file_put_contents('/path/to/your/location/report.xls', $file);
See file_get_contents and file_put_contents.

Moving images from doc root for added security

My site uses bookmarklets to gather data from external sites, kinda like Pinterest. I'm concerned about security and want to move the images the bookmarklet gathers from the doc root up one level. My script has some hefty security checks in place, but I want to add this as a last line of defense.
How do I access my images within my script? Obviously using ../userimages/id/image.jpg wont work. I'm using Apache.
Thanks!
Proxy the image
You would use a proxy script to feed the images through like the following example:
// open the file in a binary mode
$name = '../userimages/id/image.jpg';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// you may like to set some cache headers here
// dump the picture and stop the script
fpassthru($fp);
exit;
This example is from the PHP manuals fpassthru() page. You would save this script somewhere in your servers document root/httpdocs folder.
"Spoofing" the URL to the image
The easiest way to give the PHP file the appearance of being an image file to a user/browser is to use Apaches mod_rewrite. Usually I use a URL structure something like this:
http://www.example.org/image-id/image.png
Where image-id is the unique identifier for that particular image. This way the file has the correct extensions of an image instead of .php.

Categories