PHP Downloader Script Saving Files as HTML

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey there folks. I've been writing a "downloader" script that will
grab files from specified paths. It detects the MIME type just fine
and saves the file fine, but it always appends '.htm' to the end of
the filename and I can't for the life of me figure out why.

If I get rid of the .htm, the file opens correctly. This is irritating
because the filename I pass along in the content-disposition lacks
the .htm. I've tried it out in Firefox, IE7 and Chrome but get the
same results in all three.

Also, I can't do the lazy coder thing and tell my users to get rid of
the .htm. They're great people but techdumb. I'll be fielding calls
about it until Judgment Day if their files all download with a .htm

$DEBUG, $MIME_TYPES, $DOMAIN, etc are constants set in a common
library along with a few functions like is_logged_in(). With debug
turned on, the messages dump to the downloaded file (understandable)
but even with it off and nothing echo'ing out it the browser still
tries to save

Below is the (very messy, my apologies) code.

Thanks in advance,

-- Shaun

 * get_client_file.php
 *   - Grabs & returns a specified file from Z:\clients\
 * arguments:
 *   c    [required] Clients FTP UserID / Folder Name
 *   f    [required] File to download
 * returns:
 *   On Success: A file. Could be a movie, image, project data,
document, etc.
 *   On Failure: A 404 page.


/*** Preflight ***/
if ($DEBUG) { echo "running preflight"; }
// Don't have both arguments? Die a hot n' fiery death. Otherwise,
load up vars
if (!(isset($_GET['c'])) || !(isset($_GET['f'])) || !(isset($_GET
['p'])) ||
    trim($_GET['c']) == "" || trim($_GET['f']) == "" || trim($_GET
['p']) == "") {
    die("FATAL ERROR: Client ID, Path or File Name not supplied. Please
hit Back and try again.");
} else {

if ($DEBUG) { echo "checking logon"; }
// Not logged in? Boot to login page.
if (!(is_logged_in())) { header("Location: $FTP/login.php?c=$client&p=
$path&f=$file"); }

if ($DEBUG) { echo "building file path"; }
// Build file path
$full_path = $PATH_PREFIX . $client . "/" . $path . "/" . $file;
if ($DEBUG) { echo "\nPath: $full_path\n"; }

if ($DEBUG) { echo "checking file status"; }
// File doesn't exist? 404.
if (!(file_exists($full_path))) {
    header("Location: $DOMAIN/404.php?c= " . urlencode($client) . "&p=
" . urlencode($path) . "&f=" . urlencode($file));

/*** Should be good! Return the file. ***/

if ($DEBUG) { echo "starting download"; }
// Gotta tell the browser what MIME type it is first.
if (!function_exists('mime_content_type'))
        // If Fileinfo or PHP v5.3.0+ is installed
    if (function_exists('finfo_file')) {
        $fh = finfo_open(FILEINFO_MIME);
        if ($DEBUG) { echo "fileinfo extension available, checking MIME
type"; }
        $mime = finfo_file($fh,$full_path);
} else {
    // Use the deprecated function instead.
    if ($DEBUG) { echo "fileinfo extension not available, checking MIME
type with mime_content_type instead."; }
    $mime = mime_content_type($full_path);

// Guess based on extension just in case mime_content_type and
fileinfo are both missing.
if ($mime == "") {
    // Loop backwards from the end of the filename until a dot (.) is
    while(!$found) {
        if(substr($file,$i,1) == '.') {
            $extension = substr($file,$i+1);
            $found = true;
        } else { $i -= 1; }

    // check extension against mime_type array
    $mime = $MIME_TYPES[$extension];

    // if not found, default to regular binary file mimetype
    if ($mime == "") { $mime = "application/octet-stream"; }
if ($DEBUG) { echo "file: $file  --  mime-type: $mime"; }

// Start the DL
header('"Content-type: ' . $mime . '"');
header('Content-disposition: attachment; filename="'. $file . '"');
// trying an alternate method temporarily...
//$fp = fopen($full_path, 'rb');

Re: PHP Downloader Script Saving Files as HTML

SvenForkbeard wrote:

Quoted text here. Click to load it

There. Extra quotes.

Try an editor with better syntax highlighting.

Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Proudly running Debian Linux with 2.6.26-1-amd64 kernel, KDE 3.5.10, and PHP
5.2.9-1 generating this signature.
Uptime: 20:47:33 up 1 day, 23:23,  3 users,  load average: 2.65, 2.24, 2.14

Re: PHP Downloader Script Saving Files as HTML

On Apr 2, 11:48=A0am, Iv=E1n S=E1nchez Ortega <ivansanchez-...@rroba-> wrote:
Quoted text here. Click to load it

That's my fault, not the syntax highlighter's. For some reason I
spaced-out and thought that it needed to be output with double-quotes
around it. Thanks!

Re: PHP Downloader Script Saving Files as HTML

Quoted text here. Click to load it

hi try this link


Create the free account to share your knowledge with our friends ..

Thank you

Site Timeline