944,196 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7355
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 23rd, 2006
0

PHP in a html page?

Expand Post »
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:
Quote ...
$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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
martinkorner is offline Offline
102 posts
since Aug 2005
Feb 23rd, 2006
0

Re: PHP in a html page?

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:
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Feb 24th, 2006
0

Re: PHP in a html page?

How do I define the $file and $new_filename?

Martin
Reputation Points: 10
Solved Threads: 0
Junior Poster
martinkorner is offline Offline
102 posts
since Aug 2005
Feb 24th, 2006
0

Re: PHP in a html page?

[PHP]
$file = 'yourfilename';
$new_filename = 'usersfilename';[/PHP]
Reputation Points: 17
Solved Threads: 14
Posting Whiz
DanceInstructor is offline Offline
355 posts
since Feb 2005
Feb 24th, 2006
0

Re: PHP in a html page?

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
martinkorner is offline Offline
102 posts
since Aug 2005
Feb 24th, 2006
0

Re: PHP in a html page?

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]
Reputation Points: 17
Solved Threads: 14
Posting Whiz
DanceInstructor is offline Offline
355 posts
since Feb 2005
Mar 4th, 2006
0

Re: PHP in a html page?

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?
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Mar 4th, 2006
0

Re: PHP in a html page?

Quote 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
Reputation Points: 10
Solved Threads: 0
Junior Poster
martinkorner is offline Offline
102 posts
since Aug 2005
Mar 4th, 2006
0

Re: PHP in a html page?

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
Quote ...
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.
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Mar 4th, 2006
0

Re: PHP in a html page?

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
martinkorner is offline Offline
102 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: tomorrow date??? .
Next Thread in PHP Forum Timeline: Looking for an Asset Tracking system





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC