473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

outputting a file for download

im trying to let a user download a file from my website, for example a
picture. i dont want the user to have to right-click and do save-as, but
instead i want a window to popup (the window with "Save File","Open
File","More Info",etc) so that the user can download it easily. i have a
feeling this has something to do with headers, but i have no idea what the
correct headers are.

thanks in advance

- JP
Jul 17 '05 #1
4 1821
kingofkolt schrieb:
im trying to let a user download a file from my website, for example a
picture. i dont want the user to have to right-click and do save-as, but
instead i want a window to popup (the window with "Save File","Open
File","More Info",etc) so that the user can download it easily. i have a
feeling this has something to do with headers, but i have no idea what the
correct headers are.


/**
* Send a download
*
* Netscape (at least V7) (an perhaps other browsers too) adds
* a ".php" to the filename of the downloaded file. Mozilla does
* not show this behaviour. Solution (if applicable) is to use
* a link like
*
* <a href="download.php/?datei='.$file.'">
*
* The additional / does the trick.
*
*
* Other solution: use a script with no .php extension, but one
* with no ending at all. You need a special directory with a
* ".htaccess" including the line
*
* DefaultType: application/php;
*
* @param string $filename only the filename
* @param string $path path to directory of file
*/
function send_download($filename='',$path='')
{
$fullname=$path.$filename;
/*
* Headers
*/
header("Content-Type: application/octet-stream;");
header('Content-Disposition: attachment;filename='.$filename.';');
header('Content-Transfer-Encoding: binary;');
header('Content-Length: '.filesize($fullname).';');

$f=fopen($fullname,'rb');
fpassthru($f);
fclose($f);
}
I recommend using this with relatively small files (<50 MB) only.
Apache on Windows shows the behaviour of allocating RAM for the
whole size of the file, no matter what tricks you try to use in
order to prevent this (buffer-flushing during send etc).
AllOlli

Jul 17 '05 #2
> im trying to let a user download a file from my website, for example a
picture. i dont want the user to have to right-click and do save-as, but
instead i want a window to popup (the window with "Save File","Open
File","More Info",etc) so that the user can download it easily.

Before any output (otherwise headers will be sent before your code),
you could use this function for example:

promptFileDownload("http://www.site.com/file.jpg"); //you can use
either a local path or an url
The body of the function:

function promptFileDownload($chemin){
if ((!file_exists($chemin)) && (!@fclose(@fopen($chemin, "r"))))
die('Error:
invalid path to the file to download');
$filename = stripslashes(basename($chemin));
$user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
header("Content-type: application/force-download");
header(((is_integer(strpos($user_agent,
"msie")))&&(is_integer(strpos($user_agent,
"win"))))?"Content-Disposition:
filename=\"$filename\"":"Content-Disposition: attachment;
filename=\"$filename\"");
header("Content-Description: File download");
@readfile($chemin);
die();
}

(vote for it at: http://www.nexen.net/scripts/details...pts=960&vote=5)
PS: better copy/paste the code from the linked page isntead of the one
posted here because of Googlegroups's word wrapping.
Jul 17 '05 #3
"Oliver Grätz" <ol***********@gmx.de> wrote in message
news:40***********************@newsread4.arcor-online.net...

I recommend using this with relatively small files (<50 MB) only.
Apache on Windows shows the behaviour of allocating RAM for the
whole size of the file, no matter what tricks you try to use in
order to prevent this (buffer-flushing during send etc).


PHP uses memory-mapped file for file streams on Win32, I think. The file is
mapped into the application's virtual memory space, so it appears that it's
eating up a lot of memory, but only a fraction of that is physical memory.
Jul 17 '05 #4
"Cecile Muller" <sp**@wildpeaks.com> wrote in message
news:af**************************@posting.google.c om...
im trying to let a user download a file from my website, for example a
picture. i dont want the user to have to right-click and do save-as, but
instead i want a window to popup (the window with "Save File","Open
File","More Info",etc) so that the user can download it easily.

Before any output (otherwise headers will be sent before your code),
you could use this function for example:

promptFileDownload("http://www.site.com/file.jpg"); //you can use
either a local path or an url
The body of the function:

function promptFileDownload($chemin){
if ((!file_exists($chemin)) && (!@fclose(@fopen($chemin, "r"))))
die('Error:
invalid path to the file to download');
$filename = stripslashes(basename($chemin));
$user_agent = strtolower($_SERVER["HTTP_USER_AGENT"]);
header("Content-type: application/force-download");
header(((is_integer(strpos($user_agent,
"msie")))&&(is_integer(strpos($user_agent,
"win"))))?"Content-Disposition:
filename=\"$filename\"":"Content-Disposition: attachment;
filename=\"$filename\"");
header("Content-Description: File download");
@readfile($chemin);
die();
}

(vote for it at:

http://www.nexen.net/scripts/details...pts=960&vote=5)

PS: better copy/paste the code from the linked page isntead of the one
posted here because of Googlegroups's word wrapping.


ok ill try that.. thanks for the help all

- JP
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrei Pociu | last post by:
I have a major doubt about outputting text in ASP .NET when using code behind. I know most of the output you gain from a code behind file (.aspx.cs) is outputted to the Webform (.aspx) using...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
0
by: Ned Balzer | last post by:
I have a puzzling problem. I am using some code I found in another thread here to output the results of a gridview to excel. Here is the subroutine: Sub SendToExcel(ByVal Source As Object,...
0
by: Heather Barber | last post by:
Hi So I'm fine with outputting to file from C#. But... as soon as I create an executable and move the file it no longer works - in fact it crashes due to security settings of some sort - whereever i...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
5
by: phong.lee | last post by:
Hello all, I was wondering if someone can assist me in outputting 6 reports into a pdf file? I created a macro that generates the 6 reports and right now it's saved as a snapshot on my drive. ...
3
by: mohaakilla51 | last post by:
Alright guys, I am working on a flashcard script... Previously I had it so that it onlty had predefined categories. People were complaining, so now I am trying to make it to where it reads...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
22
by: g diddy | last post by:
Hi I'm relatively new to VBA and could really do with some help please!! This is going to sound really long winded i'm sorry but I hope it will paint a picture of what i'm trying to do. Basically...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.