473,322 Members | 1,714 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,322 software developers and data experts.

File Download Through Hyper Link?


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
5 5801
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bart plessers | last post by:
Hello, Currently I am developping a internet "directory browser" My page 'default.asp' has following entries: CurrentPATH = Request("MyLink") Set oFSO =...
4
by: Glenn Mulno | last post by:
Off the wall question: Is there a way to set a hyperlink so that it opens the newest file in a directory? Why: I have a page that is a link to a daily set of reports. A new report gets...
7
by: TLM | last post by:
I am trying to build a web application that will contain links to files on a users local computer. I am assuming that the files will be in a known location and can display in a browser window. ...
2
by: Otis Mukinfus | last post by:
Can someone show me the code required to download a zip file to a user? Thanks, Otis Mukinfus http://www.otismukinfus.com http://www.tomchilders.com
1
by: ramapv | last post by:
Hi to all!!!!! Please help Me!! I am having a web page which consists of three inter related windows(iframes). 1)For typing the msg to be send.(iframe1) 2)To display that msg along with the ...
3
by: ajaspersonal | last post by:
"i want to change font_style (hyper link<a href...>text</a>) normal to italics.when load a page" this is my problem but that label included in one 'USERCONTROL' This user controls may...
1
by: simons | last post by:
Hi All, I'm trying to add a hyper link to one of the filed in my DataList control. I added a link button but could not bound it to an ID filed and then link it to the page that i need. I'm...
14
by: Irfan12 | last post by:
i dont know php & i want a script that when a user click on a mp3 hyperlink, the mp3 file start downloading instead of start playing in media player. i got a sxript from internet search. i am using...
1
by: Kalaram | last post by:
I am makeing a CD, and in this CD i have alot of .pdf & .ppt & .pps files and i have one html page that i use as index to all the files, when i click on the hyper link of .pdf file , it opens...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.