/    Sign up×
Community /Pin to ProfileBookmark

Forcing download of JPG shows 0 byte file in File Explorer

OK, this is a strange one, and any help is greatly appreciated. If my File Explorer is already open and showing the folder where my file is saved, after the download, it will show size of 0 bytes. If I force a refresh (or leave the folder and come back) the filesize is accurate. Here is the really odd part…the code below USED to run in a separate browser window, (client side code was forcing a new browser to open before calling the page). It behaved properly for windows file explorer then. However, not opening a separate window is very attractive for several reasons (which I will not go into here).

I am assuming that I am missing something in either the Headers or at the end to notify the client that the send is complete, but I can’t imagine what.

Here is the code:

[code=php]<?
$filename = ‘FubNewSignature090601.jpg’;
$pathtofile = ”;

$extpos = strrpos($filename, “.”);
$extension = substr($filename, $extpos+1);

header(‘Content-Description: File Transfer’);
if ($extension == “jpg” || $extension == “jpeg”) {
header(‘Content-type: image/jpeg’);
} elseif ($extension == “bmp”) {
header(‘Content-type: image/bmp’);
} elseif ($extension == “png”) {
header(‘Content-type: image/png’);
} else {
header(“Content-type: application/octet-stream”);
}
header(“Content-Disposition: attachment; filename=$filename”);
header(‘Content-Transfer-Encoding: binary’);
header(‘Expires: 0’);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0’);
header(‘Pragma: public’);
header(‘Content-Length: ‘ . filesize($pathtofile.$filename));
ob_clean();
flush();
readfile($pathtofile.$filename);
exit;
?>[/code]

Thanks in advance for any help ?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperOct 07.2010 — I don't see anything wrong with your code, all the proper headers appear to be sent, and you told me your files explorer shows the proper filesize after refresh or close/open -- so the transfer was completed successfully.

The fact the behavior changes simply because a new browser window or tab is invoked for the transfer page has no relevancy on the page itself, in my opinion. The PHP has already been processed server side and there is no client side interaction going on based on your code as posted (i.e. no Javascript)

So what's left? A local issue with your flavor of Windows and browser.

Unless I'm missing something really obvious here. I admit I'm stumped and welcome any additional comments or suggestions from others. This is merely speculation on my part, scratching my head for sure.

-jim
×

Success!

Help @Fubster 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.18,
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,
)...