...

View Full Version : Force download .mov



mperez6109
03-19-2009, 01:01 AM
I have this file that works like a charm, but I have a problem.
In the database the download field is being inputted with the path after the root for each category, for example:

mysite.com/videos/horror/031609/horrormovie.mov
mysite.com/videos/action/031809/actionmovie.mov

so this is what is appending to it:

download.php?file=videos/horror/031809/actionmovie.mov

How can I append after .com/ "videos/horror/031609/" or "videos/action/031809/" and then the user be able to download the video.
Firefox let you download the video, but IE, and Safari prompt you to buy quicktime professional version which I don't want my users to buy any software.

Let me know if you have any questions, in advance thanks for your help.


<?php
$filename = $_GET['file'];
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title></title><body>ERROR: download file NOT SPECIFIED. USE download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title></title><body>ERROR: File not found. USE download.php?file=filepath</body></html>";
exit;
};
switch( $file_extension )
{
case "mov": $ctype="video/quicktime"; break;
case "mp4": $ctype="video/mp4"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

masterofollies
03-19-2009, 04:36 AM
Hmm I can't understand the problem you are talking about, it seems fine to me. Any more information you can provide?

kokjj87
03-19-2009, 10:52 AM
what about adding this line:



header("Content-Type: application/octet-stream");

mperez6109
03-19-2009, 05:24 PM
Hi kokjj87and masterofollies,

Thanks for replying.

Kokjj87,

The octet-stream is just another header that I am not looking for, but thanks.

masterofollies,

My information is storage in the database in this format:
so this is what is appending to it:

download.php?file=videos/horror/031809/actionmovie.mov

and then it does not work


mysql> desc myMovies;
--------------------------------------------------------------------------------+
| ID | myDate | Title | Category | myPath |
----+---------------------+--------+-----------+-------------------------------+
| 1 | 2009-03-18 00:00:00 | Jason | Horror | horror/031609/jasonmovie.mov |
| 2 | 2009-03-16 00:00:00 | Rambo | Action | action/031809/rambomovie.mov |
| 3 | 2009-03-04 00:00:00 | Taken | Action | action/031809/takenmovie.mov |
-----------+-------------+-------------------+---------------------------------+
3 rows in set (0.00 sec)


The script does works fine if would it be with out the path but this is not possible for me:


mysql> desc myMovies;
--------------------------------------------------------------------+
|ID | myDate | Title | Category | myPath |
----+---------------------+--------+-----------+-------------------+
| 1 | 2001-12-03 00:00:00 | Jason | Horror | jasonmovie.mov |
| 2 | 2001-08-24 00:00:00 | Rambo | Action | rambomovie.mov |
| 3 | 2001-08-01 00:00:00 | Taken | Action | takenmovie.mov |
-----------+-------------+---------------------+--------------------+
3 rows in set (0.00 sec)


If any body know how to deal with this problem any help would be greatly appreciated.

steelaz
03-19-2009, 05:36 PM
After


$filename = $_GET['file'];

add



// Create array [0] => videos [1] => horror [2] => 031809 [3] => actionmovie.mov
$filename = explode('/', $filename);
// Return just the file name part of the array
$filename = $filename[3];

mperez6109
03-19-2009, 05:51 PM
Hi Steelaz,

Thanks a lot, and I see you understand where I am stuck. How can the array be created?
Another problem I might face later is that, how about if I have to skip more than three directories?

steelaz
03-19-2009, 06:09 PM
The line about creating array is just a comment explaining what "explode('/', $filename);" actually does.

You should use end() function to get the last element in array. So beginning of your script should look like this:



<?php
$filename = $_GET['file'];
$filename = explode('/', $filename);
$filename = end($filename);

mperez6109
03-19-2009, 06:51 PM
ERROR: File not found. USE download.php?file=filepath
and still getting this appended to the url
mysite.com/videos/download.php?file=horror/031809/actionmovie.mov



<?php
$filename = $_GET['file'];
// Create array [0] => videos [1] => horror [2] => 031809 [3] => actionmovie.mov
$filename = explode('/', $filename);
// Return just the file name part of the array
$filename = end($filename);
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title></title><body>ERROR: download file NOT SPECIFIED. USE download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title></title><body>ERROR: File not found. USE download.php?file=filepath</body></html>";
exit;
};
switch( $file_extension )
{
case "mov": $ctype="video/quicktime"; break;
case "mp4": $ctype="video/mp4"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

steelaz
03-19-2009, 07:09 PM
Ok, let's try another thing, remove my modifications and replace this line



header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );


with



header("Content-Disposition: attachment; filename=\"".$filename."\";" );


basename() function returns just a file name, so if your videos are in sub-directories they won't be fetched.

mperez6109
03-19-2009, 07:29 PM
I changed but still appending



<?php
$filename = $_GET['file'];
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title></title><body>ERROR: download file NOT SPECIFIED. USE download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title></title><body>ERROR: File not found. USE download.php?file=filepath</body></html>";
exit;
};
switch( $file_extension )
{
case "mov": $ctype="video/quicktime"; break;
case "mp4": $ctype="video/mp4"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

steelaz
03-19-2009, 07:49 PM
What do you mean by "still appending"? What exactly happens when you access download URL, do you get any errors?

mperez6109
03-19-2009, 07:57 PM
Yes I get this error:
ERROR: File not found. USE download.php?file=filepath

In the address bar it shows like this and broken link:
mysite.com/videos/download.php?file=horror/031809/actionmovie.mov

It should be:

mysite.com/videos/horror/031809/actionmovie.mov

with download.php

be the middle man.

steelaz
03-19-2009, 08:05 PM
That's strange, I just tried the same code on my page and it worked without a problem. Obviously it can't find "actionmovie.mov" file. Are you 100% sure file is there? Can you download it when opening mysite.com/videos/horror/031809/actionmovie.mov in the browser?

mperez6109
03-19-2009, 08:19 PM
Do you think my php version (4.4.9) or probably the system administrator disable force download?

steelaz
03-19-2009, 08:26 PM
I don't think so, this error - "ERROR: File not found. USE download.php?file=filepath" means that "( ! file_exists( $filename ) )" returns TRUE, which in turn means that file with that path was not found.

mperez6109
03-19-2009, 10:10 PM
It does work the download.php when is in the same directory

I need to use it globally.

I also have this other file linktovideo.php that points to download.php and

query the database populating the link. Sample
<a href="download.php?file="<?= myPath ?>">Download video</a>

Which traslates to:

<a href="download.php?file=videos/horror/031809/actionmovie.mov">Download video</a>

steelaz
03-19-2009, 10:26 PM
If your videos are located in mysite.com/videos/genre/number/movie.mov and in database path is stored like videos/genre/number/movie.mov, then you need to move your download script from mysite.com/videos/download.php to mysite.com/download.php

mperez6109
03-19-2009, 10:54 PM
I have run to take my class right now. Do you mind if we continue tomorrow?

steelaz
03-19-2009, 10:59 PM
Sure, I'll be around.

mperez6109
03-20-2009, 09:18 PM
Steelaz,

I think I found what I was looking for, am going to give it a try, hope it works

http://apptools.com/phptools/force-download.php

The article explains at the bottom of the page.

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum