In the HTML, javascript + css forum someone told me that to make a download dialogue box pop up when a page is opened you can use the following script:

$file is the actual file (with the path included)
$new_filename is the filename that you want to send to the user. So when they download this will be the name used by the browser

the first 3 headers force the browser to download the file each and every single time, eg it's not pulling it from the memory/dish cache

the content type is just sent as some binary data type
the content-disposition tell the browser to force a download, do not open in the browser, even if you have a plugin to open it.

i use this script for images, word documents, pdfs and it works just fine.

if (is_file($file)) 
{ 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Pragma: no-cache"); 

    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=".$new_filename); 
    readfile($file); 
    exit(); 
}

How do I put this into my HTML page?

Martin

looks exactly like what I showed you...

to make a html page act like a PHP you need to make sure that your server is PHP enabled, then add the following line to your httpd.conf file:

AddType application/x-httpd-php .html

if you don't have access to that file, then just add it to a .htaccess file

the problem is, you cannot add this to an exsisting html page, this must be on a page with no other textual content, or you will get a error saying headers already sent. Just copy and paste all that code into a file and name it with a .php extension, eg: download.php

How do I define the $file and $new_filename?

Martin

$file = 'yourfilename';
$new_filename = 'usersfilename';

I do this code, then put it on my site and direct my browser to it.
But it just displays the code.?.?

Here's the contents of the .php file:

$file = 'http://www.martinkorner.host.sk/Swim To The Sea.exe';
$new_filename = 'Swim To The Sea';

{  
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
    header("Cache-Control: no-store, no-cache, must-revalidate");  
    header("Pragma: no-cache");  

    header("Content-type: application/octet-stream");  
    header("Content-Disposition: attachment; filename=".$new_filename);  
    readfile($file);  
    exit();  
}

What am I doing wrong???

Please Help...

By the way my host does support PHP because I can get the phpinfo() function to display loads of info.

Martin

Sounds like php may not be enabled on your server, or the script's file isn't named properly. Please pm me the link.

EDIT!!_____________________

Make sure you enclose any php code in php opening and closing tags. <?php "php code" ?>

<?php
$file = 'http://www.martinkorner.host.sk/Swim To The Sea.exe';
$new_filename = 'Swim To The Sea';

{  
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
    header("Cache-Control: no-store, no-cache, must-revalidate");  
    header("Pragma: no-cache");  

    header("Content-type: application/octet-stream");  
    header("Content-Disposition: attachment; filename=".$new_filename);  
    readfile($file);  
    exit();  
}
?>

martinkorner, I think you've been given some confusing help. Not incorrect help, just confusing.

From your posts, it is evident that you are not a PHP programmer and probably not even familiar with server-side web-development. So I think the examples shown you would simply confuse you.

One thing, the advice to configure you server to process HTML pages as PHP script is a bad idea--in my opinion. I think it's a bad idea for the same reason configuring SSI to work on all .html files is a bad idea. It slows down the serving of web pages. This is because normally a webserver will simply open and stream an .html file to the browser--the server does not attempt to read or process contents of the file. For PHP files, the web server must be configured to open and actually process the code, then return the HTML that results from that processed code to the browser.

What web server do you use? IIS on Windows? Apache on Windows? Apache on Linux? other?

Do you manage the web server yourself or have access to change the configuration?

I'd also like to know what your original request was. What kind of dialog do you want to pop? My assumption is that you want to have a link to download a file, but because of the file type, the file is opening in the Browser instead of prompting for a download. Is this your situation?

Sounds like php may not be enabled on your server, or the script's file isn't named properly. Please pm me the link.

EDIT!!_____________________

Make sure you enclose any php code in php opening and closing tags. <?php "php code" ?>

<?php
$file = 'http://www.martinkorner.host.sk/Swim To The Sea.exe';
$new_filename = 'Swim To The Sea';

{  
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
    header("Cache-Control: no-store, no-cache, must-revalidate");  
    header("Pragma: no-cache");  

    header("Content-type: application/octet-stream");  
    header("Content-Disposition: attachment; filename=".$new_filename);  
    readfile($file);  
    exit();  
}
?>

Thanks, this opens a download dialogue, but the downloaded file isn't anything (if you get me?)

Try going to
http://www[dot]martinkorner[dot]host[dot]sk/download.php
and you'll see what I mean.

By the way I changed 'Swim to the sea' to 'download.exe'.

(When I run the downloaded file I just get the black ms-dos box up with an error message)

Martin

Try changing

$file = 'http://www.martinkorner.host.sk/Swim To The Sea.exe';
$new_filename = 'Swim To The Sea';

To

#Adjust to wherever your file exists on your server filesystem.
$file = 'c:\webroot\somefile.exe';  //Windows
$file = '/var/www/htdocs/somefile.exe'; //or Linux

$new_filename = 'mydownload.exe';

You can use a URL as a filename, but only if supported on your server as indicated in the PHP Documentation.
From http://www.php.net/manual/en/function.readfile.php

Tip: You can use a URL as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename and Appendix M for a list of supported URL protocols.

I've put that code in and it still doesn't work.?.?

and the bit about using an URL as a filename confused me - why would I want an URL as a filename?

Martin

...and the bit about using an URL as a filename confused me - why would I want an URL as a filename?

You probably don't--that was my point. Danceinstructor gave you this example:

$file = 'http://www.martinkorner.host.sk/Swim To The Sea.exe';

My point was that you probably don't want to use a URL. You probably should reference your local file using the local filesystem instead.

Again, the help you've been shown is good, I'm just trying to clarify, but perhaps I'm not doing a good job.

You say it doesn't work, but you aren't explaining in what way it is not working--what is happening or what error messages do you see?

When things don't work, you have to break each line of code down as a single step and make sure each one is working. I usually start with the line(s) I most suspect, but if you don't know, then start at line 1. Debug each step. For example, start by doing this: (be sure to of course edit the filename to your real filename full path)

$file = '/var/www/htdocs/somefile.exe';
if (file_exists($file)) {
  echo "<br />file found";
} else {
  echo "<br />file not found";
}
if ($fh = fopen($file, 'r')) {
  echo "<br />file opened";
} else {
  echo "<br />unable to open file";
}
exit();

The idea here is, first let's make sure that your code is actually able to find the file. Second, lets make sure your code can open the file. If either of these 2 checks fail, you've got to figure out why. Really only one of 2 reasons. Either you mistyped the filename and path or the webserver user does not have privileges to access the file.

If these 2 checks work, then the code you've been given previously should work. Let us know, we'll help you get it going.

I did that and got the follwing error message:

file not found
Warning: fopen() [function.fopen]: open_basedir restriction in effect. File(/var/www/htdocs/Swim.exe) is not within the allowed path(s): (/mnt) in /mnt/storage/users/m/a/r/martinkorner/download.php on line 12

Warning: fopen(/var/www/htdocs/Swim.exe) [function.fopen]: failed to open stream: Operation not permitted in /mnt/storage/users/m/a/r/martinkorner/download.php on line 12

unable to open file

This seems to be an error with the linux code?.?'?

Martin

Martin,

I thought it would go without saying, but you need to change the path to match your actual filesystem!

So change

/var/www/htdocs/Swim.exe

To something like

/mnt/storage/users/m/a/r/martinkorner/Swim.exe

Of course your PHP code is going to have an error trying to find Swim.exe at /var/www/htdocs--that's not where your file is!

Correct the path and try the code again.

Oh right, thanks - I didn't realise.

Now it says:

file found
file opened

But no download box appears...

Martin

If you are running the troubleshooting example I gave you that looks something like this:

$file = '/var/www/htdocs/somefile.exe';
if (file_exists($file)) {
  echo "<br />file found";
} else {
  echo "<br />file not found";
}
if ($fh = fopen($file, 'r')) {
  echo "<br />file opened";
} else {
  echo "<br />unable to open file";
}
exit();

...then of course you are not seeing a download. Even if you are not a PHP programmer, you should be able to read the above code and understand that it is only testing 2 things.

  1. Can the file be found.
  2. Can the file be opened.

Then the code stops with the exit() statement. If that is not clear to you, then you may want to hire a programmer to assist you with this project. There are only 2 main commands in the above code:

  1. file_exists()
  2. fopen()

Anytime you have a question regarding what a PHP function or statement does, simply reference the excellent documentation at http://www.php.net/docs.php.

SO...you are making progress--you now have a correctly formed file path that you know works in your PHP code. Now use that file path in the original code example you started with.

Good luck.

Right...I removed the code which checks that the files exist etc, so that just the download code was there - and it worked!

Does the exit(); function stop all other codes below it, because I still had my download code below this and it didn't function at all.

Thanks
Martin

Yes exit() ends the script at whatever point it is called an any code after is not executed.

OK - thanks,

Now I've got the download working in Firefox, but in Internet Explorer the checcks come through positive, but nothing happens.

Martin

IE tries to download it for me but pops an error about an internal error in the extensions... Care to post the code you are working with now?

Yeah, it's...

<?php 
$file = 'http://www.martinkorner.host.sk/Swim.exe';  //Windows
$file = '/mnt/storage/users/m/a/r/martinkorner/Swim.exe'; //or Linux
$new_filename = '"Swim To The Sea.exe"'; 

{   
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   
    header("Cache-Control: no-store, no-cache, must-revalidate");   
    header("Pragma: no-cache");   

    header("Content-type: application/octet-stream");   
    header("Content-Disposition: attachment; filename=".$new_filename);   
    readfile($file);   
    exit();   
} 
?>

Am I supposed to have the $file specified for both Windows and Linux or just one of them?

Martin

Try the following:

<?php

$file = 'http://www.martinkorner.host.sk/Swim.exe';  //Windows
$file = '/mnt/storage/users/m/a/r/martinkorner/Swim.exe'; //or Linux
$new_filename = '"Swim To The Sea.exe"'; 

{   
    header("HTTP/1.1 200 OK");
    header("Status: 200 OK");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   
    header("Cache-Control: no-store, no-cache, must-revalidate");   
    header("Pragma: no-cache");   

    header("Content-type: application/octet-stream");   
    header("Content-Disposition: attachment; filename=".$new_filename);   
    readfile($file);   
    exit();   
} 
?>

$file = 'http://www.martinkorner.host.sk/Swim.exe'; //Windows
You understand that the line below completely overwrites the line above right?
$file = '/mnt/storage/users/m/a/r/martinkorner/Swim.exe'; //or Linux

So If I use the windows one, will linux users still be able to access the download?

Martin

The reference was to the operating system on the server, not the client-side operating system. Use the linux one. It is the one that has been being used anyway.

So If I use the windows one, will linux users still be able to access the download?

Martin

yes they will, but realisticly speaking it will do them no good, exe files are generally compiled for windows, not linux users (unless they use wine).
You would have to take that program and recompile it for a linux users (this is way more technical, and not within the scope of this forum). But yes the linux users will be able to download it.

Oh right, thanks

Martin

yes they will, but realisticly speaking it will do them no good, exe files are generally compiled for windows, not linux users (unless they use wine).
You would have to take that program and recompile it for a linux users (this is way more technical, and not within the scope of this forum). But yes the linux users will be able to download it.

Right.. thanks

Martin

I removed the space before the "<?php" tag and now it works properly in both browsers! (Thanks DanceInstructor for the pm).

At the moment that just downloads over the previous page, how would I get it to link to a html page, so for example, the user clicks a link which takes them to a page saying: "You're download should have started, if not please click here." and the download dialogue box appears.

Thanks
Martin

You don't link to a page, you just make your existing download page say that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.