file download issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

file download issues

Post by wtf »

Greets,

I'm having problems delivering consistent file name to the user that's attempting to download it.
Here's is the issue.

For example. I have file called

this is my file.doc

I can't force it to download exaclty the way it is, in this case with spaces between the words. If I try to force download, it will just offer to download file
"this" with no extention or anything. Just the first word.
I tried using rawurlencode and urlencode but in each case I'd end up with
rawurlencode => this%20is%20my%20file.doc or
urlencode => this+is+my+file.doc

I'm not sure if I'm missing something.

Code: Select all

/**
   *    [filename] => Project_Profile[1].doc
   *    [filepath] => 63.1156438918
   */

    $fpath = '/var/www/gcs/documents/' . $document['filepath'];    
    $fname = $document['filename'];
    $fsize = $document['size'];
    $buffer_size = 1024;
    
    header('HTTP/1.1 200 OK');
    header('Content-Length: ' . $fsize);
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename=' . $fname);
    header('Content-Transfer-Encoding: binary');
    
    if(file_exists( $fpath ) && $fh = fopen( $fpath, 'rb' ) )
    {
       while( $buffer = fread( $fh, $buffer_size ) ) 
       {
           echo $buffer;
       }
       
       fclose( $fh );
    }




Greatly appreciated
dubja teeff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do you have to use spaces? Can you use underscores (this_is_my_file_name.doc) or camelcase (ThisIsMyFileName.doc)?
yaqeane
Forum Newbie
Posts: 12
Joined: Wed Jun 07, 2006 8:57 am

Post by yaqeane »

Everah wrote:Do you have to use spaces? Can you use underscores (this_is_my_file_name.doc) or camelcase (ThisIsMyFileName.doc)?
yerps that right use space or no space
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Nope... I don't have to use spaces or anything else for that matter. It's not a big deal at all. I just thought it would be nice to save file with the same name it was uploaded.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If memory serves, one can place quotes around the filename in the header.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Your memory servs you well...

Code: Select all

header('Content-Disposition: attachment; filename="' . $fname . '"');

thx
Post Reply