| | |
PHP in a html page?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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:
How do I put this into my HTML page?
Martin
•
•
•
•
$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]
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: 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
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:
PHP Syntax (Toggle Plain Text)
AddType application/x-httpd-php .html
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
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
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
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]
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]
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?
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?
•
•
•
•
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]
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
[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
[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.
![]() |
Similar Threads
- Can I use a template html page within another html page (Site Layout and Usability)
- php contact page (PHP)
- How To Hyperlink Normal HTML page with ASP.NET Page? (ASP.NET)
- How to Embed CheckList Box on HTML Page using Java Script (HTML and CSS)
- Calling function to add HTML to the page (ASP.NET)
- Use php to load a page in a page? (PHP)
Other Threads in the PHP Forum
- Previous Thread: tomorrow date??? .
- Next Thread: Looking for an Asset Tracking system
| Thread Tools | Search this Thread |
301 access apache api array beginner binary broken button cakephp checkbox class clean cms code countingeverycharactersfromastring crack cron curl database date decode directory display dissertation dropdown dynamic echo email error fairness file files folder form forms function functions google href htaccess html image include insert integration ip javascript joomla limit link login mail match md5 menu methods mlm multiple mysql newsletters oop pageing pagerank paypal pdf php problem protocol query radio random recursion remote script search secure server sessions simple sms soap source space spam sql syntax system table tutorial update upload url validator variable video virus votedown web youtube





