FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to make a website do 'Save as'





svecia
How do You make output to a file from a website so the browser promts
to save the file ?
That is like if You put a file with name nn.exe on the webserver
and the user are propted to run it or download.
But now I have ' the file ' in a blob in a mysql table and wants a
php-script to do the same thing . Is it possible to do that without
saving any temporary file on the server ?
hexkid
svecia wrote:
How do You make output to a file from a website so the browser promts to save the file ?


use the header() function to specify the MIME type of the data
Code:
<?php
header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename="example.txt"');
echo 'An example text file as download.';
?>

The code above may not work for all browsers.
You may want to try different MIME types and filenames.
Marston
It's extremely suspicious that he wants to prompt the browser to save an EXE file...
vln004
Marston wrote:
It's extremely suspicious that he wants to prompt the browser to save an EXE file...


i suggest you reread his post.
rohan2kool
hexkid wrote:

The code above may not work for all browsers.
You may want to try different MIME types and filenames.


It does work in most browser's i have tried.. but the php documentation points out that Content-disposition: attachment header doesn't work in IE 5.5 due to a bug in it. In IE 5.5, the entire file is outputted to the stream and u can see the file in ur browser (i mean if it's an exe.. u can see the garbled characters).

Also you can try this.. but it's pretty silly sorta thing:

Code:

<?php
//In the example, $fdata contains the data of the file
//Which you can get from your MySQL database
//I am just explaining what to do next
$fdata = "This is a text file";
$fname = "file.txt";

if(is_writeable($fname)) {
   if(!$handle = fopen($fname, 'a')) {
     echo "Cannot open file... Read error";
     exit();
   }

   if(fwrite($handle, $fdata) === false) {
     echo "Write error";
   }

   header("Location: ".$fname);
   exit();
} else {
    echo "File is not writeable";
}
?>


what it does is just take the data from any source, write a fresh file to it and then give it as a download link. Also, you would like to clear the files once the download is complete and that's a bit of problem... otherwise you can run a cron job which checks for files older than say a day or two and delete them.. but that pretty much breaks the whole purpose of it. Maybe some other frihoster here can extend my idea which just might help you Smile
krazycapital
Put all the downloads in one folder, and then just put this line in the .htaccess:
Code:
AddType application/octet-stream

And it will force download for everything in that folder. Just a normal link, and it won't lose the page they're on either.
rohan2kool
krazycapital wrote:
Put all the downloads in one folder, and then just put this line in the .htaccess:
Code:
AddType application/octet-stream

And it will force download for everything in that folder. Just a normal link, and it won't lose the page they're on either.


So that's what an octet-stream type MIME is for?? thanks.. have always been wondering (and also too lazy to check out wpedia fer dat Wink )
Related topics
Save the Best Smileys Website
Simple ways to bost your Website Traffic!
Prevent my website from being downloaded
How to make a Website....
Save your codes from people
I am broke
How to save HTML in Databases wise ly ?
changes dont save?
Save file on local floppy (perl)
Excel SpreadSheet to Website
Website Design
Building a Website Offline
How do I make a template?
How can I disable my website to save points
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.