php headers instant download videos [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Force-downloading, from php file
(2 answers)
Closed 9 years ago.
I am trying to make a php script when you load on to that page it downloads a video. However since i do not know anything about headers it seems i can't figure it out so please explain why it works and how header works. I am trying to make the browser download video files.Can someone also explaain the headers and what they do please.
Here is my failing code:
<?php
//Outputing video name
$file_name = $_POST['FileToD'];
//outputting video extension e.g video/mp4
$file_ext= $_POST['FileExt'];
//where the file is kept
$file_path = 'mysever.myadress.com/media/movies/' . $file_name;
header('Content-Type:'.$file_ext);
header('Content-Length:' . filesize($file_path));
header('Content-Description: attachment; filename='.$file_name);
readfile($file_path);
?>

If you want to output a video, then don't start by outputting HTML and then switch to video data as part of the same file. (You can't set response headers after you've started outputting data anyway). Remove everything before <?php and after ?>
$file_url should be the path, on the server's file system, to the file you want to make available. It shouldn't be a URL (unless you want a really inefficient approach or need to proxy from a different server), and if it is a URL then it needs to start with the scheme (e.g. http://).
The content-type needs to be the actual content type of the video (e.g. video/mp4), not a file extension (and it doesn't make sense for it to be provided by the user).
You also need to sanitise the user data. At present (if the errors described above were fixed) then anybody could request any file that exists on the server.

Related

PHP insert image from outside document root [duplicate]

This question already has answers here:
How to serve documents from outside the web root using PHP?
(2 answers)
Closed 8 years ago.
Okay, so I'm not so experienced with PHP, and I've been searching for hours for a way to access an image file outside of the document root. I know there are many answers to this question, sort of, but none that actually helped me.
So what I have so far is a folder structure like this (ignore the odd file names):
-img
-imagez.php
-logo.php
-public_html
-files.php
I put this code inside of files.php:
<?php include('/home/byonexco/img/imagez.php'); ?>
If I access files.php from my browser, I see the content of imagez.php, as is expected.
My problem is, I want to be able to do the same thing with the file logo.png. The folder img is not publicly accessible, so I know I have to call the image with PHP.
How can I get logo.png to show on the page when someone accesses the file files.php?
You could write a very simple script like
<?php
header('Content-Type: image/png');
if (strpos($_GET['img'], '..') === false) // check for quackery
readfile('../img/' . $_GET['img']);
and access it like
/img.php?img=logo.png
However there are a couple disadvantages to this solution:
Relaying the image through PHP costs time and performance
The script is possibly subjectible to exploits, letting an evil person retrieve any file on the server
You're far better off with hosting images directly accessible.
As the image isn't publicly accessible, you'd need to get the image via PHP then output the image with the correct header
$image = file_get_contents('path/to/image.png';
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

Which header to set or how to download a php file [duplicate]

This question already has an answer here:
php: force download to hd?
(1 answer)
Closed 9 years ago.
i was trying to download from an android application a php file stored onto my server.
From the application i call a webservices that have to give me in output the file .php that i need.
On the web i found this that talk about which header set to download a file. It talks about
zip
jpg
txt
pdf
Then i was thinking to develop something like:
Application call ws to get a php page
Ws zips the php file that application needs and give me in putput
Application download the zip file and extract it.
This is a good solution but i was trying to find something better.
Another solution that is really like what i want is highlight_file
Just use it like:
echo highlight_file("myphpfile.php");
The problem is that in order to render good the file, the code is divided by many html tags and just "visually" is the same file but the source is really different.
Is there a way to download the file directly? Thanks! :)
The simplest solution might be:
<?php
header('Content-Type: text/plain');
readfile("myphpfile.php");
this will display the file as text in browser you can click 'save as...'
If you need the browser to popup a 'Save as...' dialog by itself - without displaying the file - then you'll need the Content-Disposition header:
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="myphpfile.php"');
readfile("myphpfile.php");

create hidden path to a file [duplicate]

This question already has answers here:
A PHP script to let users download a file from my website without revealing the actual file link in my website?
(4 answers)
Closed 9 years ago.
I want to make images shopping site in which I want people buy images then they can download them.
My problem is how to create hidden path to image that people download the image and don't know the real path of the image.
You can call a php file to download the image and not the real image/path.
Like this you can call the real path inside your php file with something like:
$path = "/public_html/yourPath/";
if (! isset($_GET['img'])) {
die("Invalid URL");
}
$imageName = filter_var($_GET['img'], FILTER_SANITIZE_STRING);
$finalPath = $path.$imageName;
header('Content-type: octet/stream');
header('Content-Type: image/jpg');
header("Content-Disposition: attachment; filename=$finalPath;");
readfile($finalPath);
You can read more about it here.
Store the images in an offline location (not www) and retreive them with PHP, so they can access the image for example like this: http://yoursite.com/index.php?file=filename and then PHP will go and return that file from the offline location. You just need to set the correct headers so the content is not treated like a web page but an image instead. Now obviously, such link is still public so you need to add some more information to it to authenticate the downloader.

Download a file which opens in the browser instead of downloading [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to force download of a file?
How to download a file that directly opens in the browser by default.
For ex- In older browsers, if I opened a link of a mp3 song, then it were downloaded; but now-a-days, they start playing in the browser itself. One method is to right-click the link and click save link as and then save the song; but many times, the link provided is a mask to some other link which is actually the song. So what to do in this case?
You need to set the content disposition to attachment in the header like this:
header('Content-Disposition: attachment');
Also to Include a file name you can use :
header('Content-Disposition: attachment; filename=abc.mp3');
Or maybe configure apache[if you're on apache] to have this defined for filetypes you want to force download, like
<FilesMatch "\.(mp3|mov|pdf)">
Header set Content-Disposition attachment
</FilesMatch>

Forcing page download in PHP [duplicate]

This question already has answers here:
How to force file download with PHP
(12 answers)
Closed 2 years ago.
How do I force the browser to download the current page browsed to? A page with the header Content-type: text/plain for example using PHP?
If a user navigates to that page, a download box should appear (the browser download dialog with the usual "Save As".
Straight from http://php.net/header
<?php
// There is contention over if this MIME type is right, but just use it for now.
header('Content-type: text/javascript');
header('Content-Disposition: attachment; filename="file.js"');
readfile('file.js'); // Echo the file
?>
NOTE: this must be done before any other output (and can be about the only thing on the page, unless you want other output in your file).

Categories