/    Sign up×
Community /Pin to ProfileBookmark

trying to hide file location for word docs.

This is the calling form:

[code=php]
function display_docs($class_num) {
echo “<div id=’doc_files’>n<ul>n”;
$folder = “word_documents/$class_num/”;
$_SESSION[‘location’] = $folder;
$handle = opendir($folder); #opens the directory so we can read the files out
while($file = readdir($handle)){ #loop through the files
$ext = substr($file, -4);
if($ext == “.doc”){
echo “<li><a href=’download.php?id=$file’>$file</a></li>n”;
}
}
[/code]

and this is the download.php, which seems to work in FF but when I try to open the file in IE it changes the file name so it won’t open, it will download and open fine, but it doesnt open in the browser.

[code=php]
session_start();

if (!isset($_GET[‘id’]) || !file_exists($_SESSION[‘location’].$_GET[‘id’])) {
die(‘The document does not exist.’);
}

$file_location = $_SESSION[‘location’].$_GET[‘id’];

header(‘Content-type: file/.doc’);
header(‘Content-Disposition: attachment; filename=”‘.basename($_GET[‘id’]).'”‘);
echo implode(null, file($file_location));

[/code]

Ok, so what i’m asking is this: is this the correct coding to perform this action?

In addition, if I have 40 or 400 docs and I would like to offer them all to download, do I have to manually place them in a zip file to be included as a download link or can php zip all the files in the directory and send them?

thanks.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 09.2006 — Might want to use this for the mime type:
[code=php]
header('Content-type: application/msword');
[/code]
Copy linkTweet thisAlerts:
@rch10007authorMar 09.2006 — thx charles,

i wasnt sure about all the content types and i cant seem to locate a comprehensive list.

also, do you know why (in IE) the file name gets changed from "1.01.01.doc" to "1[1].01.01.doc"?

by IE changing the file name for download, it prohibits me from opening the file in IE - however it downloads and tehn opens just fine.
Copy linkTweet thisAlerts:
@NogDogMar 09.2006 — Maybe you already have a copy in the download directory (or one of the temp files), so it's automatically making a copy of it? I'm just guessing, but you could verify it by deleting all temporary internet files, then trying it again. (Tools -> Internet Options -> General -> Delete Files)
Copy linkTweet thisAlerts:
@rch10007authorMar 09.2006 — i tried to clear out all the local docs, but IE still changes the name??

hey - do have any ideas about whether or not php can zip all the files in a given directory for download instead of having to download them individually?
Copy linkTweet thisAlerts:
@rch10007authorMar 09.2006 — Ok - so i changed my download.php file to look like this and it seems to open the file without downloading it now.

I guess I didn't have enough headers:

[code=php]
session_start();

if (!isset($_GET['id']) || !file_exists($_SESSION['location'].$_GET['id'])) {
die('The document does not exist.');
}

$file_location = $_SESSION['location'].$_GET['id'];
session_write_close();

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: application/msword");
header("Content-Disposition: attachment; filename=".$_GET['id']);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file_location));

readfile($file_location);

[/code]


Hey - i still need some thoughts about providing 1 link to get all the files in the directory - any thoughts?

I hate to constantly update a zip file when i modify the docs. There's a programmers solution somewhere ?
×

Success!

Help @rch10007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...