# Force file download



## dannyn (Nov 9, 2007)

I need help with force file download for asp.
Thanks.


----------



## TheRobatron (Oct 25, 2007)

Try looking here.


----------



## dannyn (Nov 9, 2007)

so should i use the first or second example? 
I really need help with this, and I am only 15 years old, and this is the first time i have worked with asp.
Thanks alot!


----------



## TheRobatron (Oct 25, 2007)

I'm not very familiar with ASP, so I don't know which example you should use, but try both and see which works best.


----------



## dannyn (Nov 9, 2007)

neither, thats my problem.
http://senior.bpsepforums.org/ thats where it is.
thanks!


----------



## TheRobatron (Oct 25, 2007)

Have you considered using PHP? It seems simpler and I understand the code:

```
<?
function force_download($file)
{
    $dir      = "../log/exports/";
    if ((isset($file))&&(file_exists($dir.$file))) {
       header("Content-type: application/force-download");
       header('Content-Disposition: inline; filename="' . $dir.$file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($dir.$file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$dir$file");
    } else {
       echo "No file selected";
    } //end if

}//end function
?>
```


----------



## TheRobatron (Oct 25, 2007)

Have you considered using PHP? It seems simpler and I understand the code:

```
<?
function force_download($file)
{
    $dir      = "../log/exports/";
    if ((isset($file))&&(file_exists($dir.$file))) {
       header("Content-type: application/force-download");
       header('Content-Disposition: inline; filename="' . $dir.$file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($dir.$file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$dir$file");
    } else {
       echo "No file selected";
    } //end if

}//end function
?>
```


----------



## dannyn (Nov 9, 2007)

I have asp servers...
It is a possibility though.
I will need help on where to put the file path and such.


----------



## Eriksrocks (Aug 7, 2005)

The general concept to this is to force the MIME type of _application/force-download_ or _application/octet-stream_ through the headers of the page. The example TheRobatron posted is how to do it in PHP, you'll need to accomplish the same thing in ASP. As I'm not familiar with ASP, I wouldn't know the exact code to write, but you're going for the same concept.


----------



## dannyn (Nov 9, 2007)

Yea, I understand what I am trying to do.
I just needed someone to write the code for asp, i really need this so i am thinking of switching one of my servers over to Linux so i can run the code.
Thanks for the responses.


----------



## MMJ (Oct 15, 2006)

PHP: http://www.php.net/manual/en/function.readfile.php#76850

ASP: http://www.bellaonline.com/articles/art46052.asp //it has some extra code though.


----------



## dannyn (Nov 9, 2007)

The robatron what variables do i have to replace? 
I am using php 4


----------



## TheRobatron (Oct 25, 2007)

All you would have to change is the $dir variable to the folder you want to download to, relative to the page. *Note:* you can just leave it blank and give the full file path when you call the function.

```
<?
function force_download($file)
{
    $dir      = "../log/exports/";
    if ((isset($file))&&(file_exists($dir.$file))) {
       header("Content-type: application/force-download");
       header('Content-Disposition: inline; filename="' . $dir.$file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($dir.$file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$dir$file");
    } else {
       echo "No file selected";
    } //end if

}//end function
?>
```
And when you call the function you specify the file:

```
<?php
force_download("file.jpg");
?>
```


----------



## dannyn (Nov 9, 2007)

<?
$filename = "http://bpsepforums.org/Storage/Final.mov";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header('Content-Disposition: attachment; filename="Final.mov";');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile($filename);
exit();
?>

thats what i have, now what do i do with it. 
I dont understand this to much.
How will filename work if it is on a differnt computer


----------



## Eriksrocks (Aug 7, 2005)

You put it in a PHP script and run it or link to it on your website.


----------



## dannyn (Nov 9, 2007)

with this? <?php
force_download("file.jpg");
?>

or an html tag?


----------



## Eriksrocks (Aug 7, 2005)

Nope, just the script that you posted in your last post, if that's what you're using. You would put that in a file called downloadmovie.php or something and then you would link to it wherever you want users to download the MOV file.

So it might look like:


```
[URL=downloadmovie.php]Download my movie here![/URL]
```
Then when they click on that it would force download the file that you specified earlier.

Does that make sense? 

EDIT: Also, don't know what you're doing with this line:

```
header("Content-Type: $ctype");
```
You never decalred the $ctype variable anywhere in the script, and that's sort of the main part in making it force the download.


----------



## dannyn (Nov 9, 2007)

Using this code now.

```
<?PHP
// Define the path to file
$file = 'test.mp4';

if(!file)
{
    // File doesn't exist, output error
    die('file not found');
}
else
{
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");

    // Read the file from disk
    readfile($file);
}
?>
```
senior.danjnelson.com
Working sort of as you may see.
Can I choose the name the file will be downloaded as?
I can use the other code, I just am going to need more instruction.
Thanks!!


----------



## TheRobatron (Oct 25, 2007)

I think to choose the name it is downloaded as, you'll have to change the name of the file on the webserver. The code seems to work, so is that your problem sorted?


----------



## dannyn (Nov 9, 2007)

have you tried to downlaod it?
Using mozilla it names the location then the file, and it fails to replay after its downloaded. 
I will try your script, but where do i define the file path?


----------



## Eriksrocks (Aug 7, 2005)

Why are you setting the content type as a ZIP?

```
header("Content-Type: application/zip");
```
That makes the browser think it's a ZIP file, which it's not. 
I tried to download it and play it in QuickTime, but it wouldn't play. That's not a problem with the script, however, it's a problem with the original file or the way you uploaded it to the server.


----------



## TheRobatron (Oct 25, 2007)

I assumed that when I downloaded it, the file would play (which it doesn't now ). With my code, you change the $dir variable to specify the folder, and then specify the filename when you call the function. To download a file called test.txt in the current folder, you would use the following code:

```
<?php
function force_download($file)
{
    $dir      = "./";
    if ((isset($file))&&(file_exists($dir.$file))) {
       header("Content-type: application/force-download");
       header('Content-Disposition: inline; filename="' . $dir.$file . '"');
       header("Content-Transfer-Encoding: Binary");
       header("Content-length: ".filesize($dir.$file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$dir$file");
    } else {
       echo "No file selected";
    } //end if

}//end function

force_download("test.txt");
?>
```
The $dir variable and the $file variable are joined together anyway, so it doesn't matter if you want to specify the whole file path when you call the function.

Note: As before, you will have to put the code in a separate file.


----------



## Eriksrocks (Aug 7, 2005)

I would recommend trying the script with a simple JPG file or even a TXT file to make sure that it's working right and that the problem isn't the MP4.


----------



## dannyn (Nov 9, 2007)

Thanks for all your help.
You guys are amazing. 
Changed to header('Content-Type: '.$mime);
It worked.
Thanks.


----------



## dannyn (Nov 9, 2007)

This is not necessary, but how would I let the download manager know the size of the file?
Thanks


----------



## Eriksrocks (Aug 7, 2005)

dannyn said:


> This is not necessary, but how would I let the download manager know the size of the file?
> Thanks


Add this where the rest of the headers are:

```
header("Content-length: ".filesize($dir.$file));
```


----------



## dannyn (Nov 9, 2007)

```
<?PHP
// Define the path to file
$file = 'test4.mp4';

if(!file)
{
    // File doesn't exist, output error
    die('file not found');
}
else
{
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header('Content-Type: '.$mime);
    header("Content-Transfer-Encoding: binary");

    // Read the file from disk
    readfile($file);
}
?>
```
There is no $dir, but i will still try it. 
Do i still want the encoding to be binary? I am thinking yes, beacuse as the code stands, it works.
Thanks


----------



## TheRobatron (Oct 25, 2007)

Without the $dir variable, it would just be

```
header("Content-length: ".filesize($file));
```


----------



## dannyn (Nov 9, 2007)

Ok. Thanks. I will try that when I get home. 
Thanks for all of your help!


----------



## dannyn (Nov 9, 2007)

Alright,Got it going due to all your help.
Thanks!
I am going to post the final code one more time in case someone else would like to use it.

```
<?PHP
// Define the path to file
$file = 'test4.mp4';

if(!file)
{
    // File doesn't exist, output error
    die('file not found');
}
else
{
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header('Content-Type: '.$mime);
    header("Content-Transfer-Encoding: binary");
	header("Content-length: ".filesize($file));

    // Read the file from disk
    readfile($file);
}
?>
```


----------



## TheRobatron (Oct 25, 2007)

Glad you got it working 
I'll save that code in case I ever need to use it in the future.


----------

