PHP in a html page?

Reply

Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

PHP in a html page?

 
0
  #1
Feb 23rd, 2006
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.


[PHP] 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();
} [/PHP]
How do I put this into my HTML page?

Martin
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: PHP in a html page?

 
0
  #2
Feb 23rd, 2006
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:
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: PHP in a html page?

 
0
  #3
Feb 24th, 2006
How do I define the $file and $new_filename?

Martin
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: PHP in a html page?

 
0
  #4
Feb 24th, 2006
[PHP]
$file = 'yourfilename';
$new_filename = 'usersfilename';[/PHP]
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: PHP in a html page?

 
0
  #5
Feb 24th, 2006
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:
[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();
} [/PHP]

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: PHP in a html page?

 
0
  #6
Feb 24th, 2006
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]<?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();
}
?>[/PHP]
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: PHP in a html page?

 
0
  #7
Mar 4th, 2006
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?
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: PHP in a html page?

 
0
  #8
Mar 4th, 2006
Originally Posted by DanceInstructor
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]<?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();
}
?>[/PHP]
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: PHP in a html page?

 
0
  #9
Mar 4th, 2006
Try changing
[PHP]$file = 'http://www.martinkorner.host.sk/Swim To The Sea.exe';
$new_filename = 'Swim To The Sea';
[/PHP]
To
[PHP]#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';
[/PHP]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.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 102
Reputation: martinkorner is an unknown quantity at this point 
Solved Threads: 0
martinkorner's Avatar
martinkorner martinkorner is offline Offline
Junior Poster

Re: PHP in a html page?

 
0
  #10
Mar 4th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC