By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

File Download Through Hyper Link?

P: n/a

I'd like to write to a log whenever a visitor to my site downloads a
file. So, I'd like the link they click to be to a php 'page' that
returns the file. Don't know how to do that!

Side Question ...

Presumably, the dialog that you see when you download a binary (asking
for a 'Save As...' file name) is put up by your browser when it 'sees'
non-text data coming back as a response? So, how would you download a
text file - and cause this dialog to appear at the user's end?

Thanks!

pemo
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Wed, 26 Nov 2003 10:49:12 GMT, peetm <jg********@blueyonder.co.uk>
wrote:

I'd like to write to a log whenever a visitor to my site downloads a
file. So, I'd like the link they click to be to a php 'page' that
returns the file. Don't know how to do that!

Just pass to the php page an id for the file that has to download and
then redirect (with javascript for example) to the file.
Side Question ...

Presumably, the dialog that you see when you download a binary (asking
for a 'Save As...' file name) is put up by your browser when it 'sees'
non-text data coming back as a response? So, how would you download a
text file - and cause this dialog to appear at the user's end?

Well, i think that this dialog doesnt appear because the file is
binary, he appears because the browser doesnt know how "manage" that
file. Files with extension swf for example are binaries and the
browser doesnt show that dialog.

If you want to log the acces to that files (txt files) just redirect
to this files from the php page too. But if you want to show the
dialog i dont know, maybe with javascript you can, has to be something
like save the page.
Thanks!

pemo


Not at all, and sorry my english ;-)
Jul 17 '05 #2

P: n/a
Looking at php.net I thought this would work ...

<?php

$filename='./' . $_GET['name'];

$fp=fopen($filename, "r");

$contents = fread ($fp, filesize($filename));

fclose($fp);

@readfile($filename);

// just does the same.
//
//echo $contents;

?>

but I just get a load of garbage as a result (the binary 'made text'
really)

pemo
On Wed, 26 Nov 2003 10:49:12 GMT, peetm <jg********@blueyonder.co.uk>
wrote:

I'd like to write to a log whenever a visitor to my site downloads a
file. So, I'd like the link they click to be to a php 'page' that
returns the file. Don't know how to do that!

Side Question ...

Presumably, the dialog that you see when you download a binary (asking
for a 'Save As...' file name) is put up by your browser when it 'sees'
non-text data coming back as a response? So, how would you download a
text file - and cause this dialog to appear at the user's end?

Thanks!

pemo


Jul 17 '05 #3

P: n/a
You need to get the appropriate HTTP header so that IE doesn't think
that it's an HTML page/

<?php

$filename = $_GET['filename'];
$path = "/somewhere";

/* log the download here */

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$filename");

readfile("$path/$filename");

?>

You can also do a HTTP redirect to the file

<?php

$filename = $_GET['filename'];
$url_root = "/somewhere";

/* log the download here */

header("Location: $url_root/$filename");

?>

This is safer than the first method, as in this case the web server
controls which files the user has access to. Doing a readfile() means
having to do that yourself in order to ensure that you're not exposing
files not meant for the public (e.g. PHP files).

peetm <jg********@blueyonder.co.uk> wrote in message news:<6m********************************@4ax.com>. ..
Looking at php.net I thought this would work ...

<?php

$filename='./' . $_GET['name'];

$fp=fopen($filename, "r");

$contents = fread ($fp, filesize($filename));

fclose($fp);

@readfile($filename);

// just does the same.
//
//echo $contents;

?>

but I just get a load of garbage as a result (the binary 'made text'
really)

pemo
On Wed, 26 Nov 2003 10:49:12 GMT, peetm <jg********@blueyonder.co.uk>
wrote:

I'd like to write to a log whenever a visitor to my site downloads a
file. So, I'd like the link they click to be to a php 'page' that
returns the file. Don't know how to do that!

Side Question ...

Presumably, the dialog that you see when you download a binary (asking
for a 'Save As...' file name) is put up by your browser when it 'sees'
non-text data coming back as a response? So, how would you download a
text file - and cause this dialog to appear at the user's end?

Thanks!

pemo

Jul 17 '05 #4

P: n/a
The following (below) worked - although the 'Save As' dialog prompts
the user to save the file as 'files' (that's the name of my php page,
e.g., http://www.blah.com/files.php?name=thing.zip) and not as
'thing.zip' - is there anyway to correct that?

(btw, I didn't know you could 'quote' a variable like that
(readfile("$filename");) - is there any difference between
readfile("$filename"); and readfile($filename);

pemo

<?php

$filename='./' . $_GET['name'];

/* log the download here */

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$filename");

readfile("$filename");

?>

On 26 Nov 2003 21:38:07 -0800, ch***********@hotmail.com (Chung Leong)
wrote:
You need to get the appropriate HTTP header so that IE doesn't think
that it's an HTML page/

<?php

$filename = $_GET['filename'];
$path = "/somewhere";

/* log the download here */

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$filename");

readfile("$path/$filename");

?>

You can also do a HTTP redirect to the file

<?php

$filename = $_GET['filename'];
$url_root = "/somewhere";

/* log the download here */

header("Location: $url_root/$filename");

?>

This is safer than the first method, as in this case the web server
controls which files the user has access to. Doing a readfile() means
having to do that yourself in order to ensure that you're not exposing
files not meant for the public (e.g. PHP files).

peetm <jg********@blueyonder.co.uk> wrote in message news:<6m********************************@4ax.com>. ..
Looking at php.net I thought this would work ...

<?php

$filename='./' . $_GET['name'];

$fp=fopen($filename, "r");

$contents = fread ($fp, filesize($filename));

fclose($fp);

@readfile($filename);

// just does the same.
//
//echo $contents;

?>

but I just get a load of garbage as a result (the binary 'made text'
really)

pemo
On Wed, 26 Nov 2003 10:49:12 GMT, peetm <jg********@blueyonder.co.uk>
wrote:
>
>I'd like to write to a log whenever a visitor to my site downloads a
>file. So, I'd like the link they click to be to a php 'page' that
>returns the file. Don't know how to do that!
>
>Side Question ...
>
>Presumably, the dialog that you see when you download a binary (asking
>for a 'Save As...' file name) is put up by your browser when it 'sees'
>non-text data coming back as a response? So, how would you download a
>text file - and cause this dialog to appear at the user's end?
>
>Thanks!
>
>pemo


Jul 17 '05 #5

P: n/a
Which browser are you using? IE 5.5 has a bug which causes the
syndrome you described. See this knowledge base article:

http://support.microsoft.com/default...b;en-us;303750

Check the PHP manual more info about inserting variables into strings:

http://www.php.net/manual/en/languag....syntax.double

One thing I forgot to mention in my last post is that if auto session
is on, you would need to call session_write_close() before calling
readfile(). Otherwise your visitors wouldn't be able to go to other
parts of your site while the download is occurring (as the session is
locked).

pemo <jg********@blueyonder.co.uk> wrote in message news:<0b********************************@4ax.com>. ..
The following (below) worked - although the 'Save As' dialog prompts
the user to save the file as 'files' (that's the name of my php page,
e.g., http://www.blah.com/files.php?name=thing.zip) and not as
'thing.zip' - is there anyway to correct that?

(btw, I didn't know you could 'quote' a variable like that
(readfile("$filename");) - is there any difference between
readfile("$filename"); and readfile($filename);

pemo

<?php

$filename='./' . $_GET['name'];

/* log the download here */

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$filename");

readfile("$filename");

?>

On 26 Nov 2003 21:38:07 -0800, ch***********@hotmail.com (Chung Leong)
wrote:
You need to get the appropriate HTTP header so that IE doesn't think
that it's an HTML page/

<?php

$filename = $_GET['filename'];
$path = "/somewhere";

/* log the download here */

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$filename");

readfile("$path/$filename");

?>

You can also do a HTTP redirect to the file

<?php

$filename = $_GET['filename'];
$url_root = "/somewhere";

/* log the download here */

header("Location: $url_root/$filename");

?>

This is safer than the first method, as in this case the web server
controls which files the user has access to. Doing a readfile() means
having to do that yourself in order to ensure that you're not exposing
files not meant for the public (e.g. PHP files).

peetm <jg********@blueyonder.co.uk> wrote in message news:<6m********************************@4ax.com>. ..
Looking at php.net I thought this would work ...

<?php

$filename='./' . $_GET['name'];

$fp=fopen($filename, "r");

$contents = fread ($fp, filesize($filename));

fclose($fp);

@readfile($filename);

// just does the same.
//
//echo $contents;

?>

but I just get a load of garbage as a result (the binary 'made text'
really)

pemo
On Wed, 26 Nov 2003 10:49:12 GMT, peetm <jg********@blueyonder.co.uk>
wrote:

>
>I'd like to write to a log whenever a visitor to my site downloads a
>file. So, I'd like the link they click to be to a php 'page' that
>returns the file. Don't know how to do that!
>
>Side Question ...
>
>Presumably, the dialog that you see when you download a binary (asking
>for a 'Save As...' file name) is put up by your browser when it 'sees'
>non-text data coming back as a response? So, how would you download a
>text file - and cause this dialog to appear at the user's end?
>
>Thanks!
>
>pemo

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.