tags:

views:

157

answers:

4

Given: All the uploaded pdf files on server are prefixed with timestamps. Later user can download these files again. These (ugly)filenames would never change again on server.

Question: When I give the option to download PDF file, name of the file looks ugly and lengthy. How can I change this name to something sensible, so that when user downloads this file, name doesn't look weird?

Do I need to make a copy, since renaming the original file is not an option? Wouldn't it be extra overhead for each downloadable file? Obviously deleting the copied file would be another extra step?

Is it possible to rename file once file is completely downloaded on client side?

What do you guys suggest?

+3  A: 

Something like this:

<?php

// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf'); ?>

klausbyskov
`Content-type` should be `Content-Type`, though.
jensgram
@jensgram: it's not case sensitive.
BalusC
@klausbyskov: please see the comment below by BalusC. according to him it won't work on IE?
understack
I also found this thread http://stackoverflow.com/questions/698308/why-doesnt-content-disposition-header-work-in-ie-8 useful
understack
@BalusC: You seem to be right :) http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
jensgram
I have never heard that it doesn't work in IE, but try it out to make sure that it does.
klausbyskov
A: 

The filename of the content served is defined in the HTTP headers. Look around the PHP page on headers (also the comments) for more info.

DrJokepu
A: 

You need to set the desired filename in the Content-Disposition header:

$name = 'desiredname.pdf';
header('Content-Disposition: attachment;filename="' . $name . '"');

But unfortunately this won't be picked up by a certain webbrowser developed by a team in Redmond. If you want to get it to work in that browser as well, then you need to append the very same filename as last part of the request path. For example: http://example.com/pdf/desiredname.pdf.

BalusC
I think that technically the filename parameter should not be in quotation marks. If the name contains no spaces or funny characters, leave it out of quotation marks.
TRiG
@BalusC: this was helpful like http://stackoverflow.com/questions/698308/why-doesnt-content-disposition-header-work-in-ie-8 this one. Thanks.
understack
A: 

I was doing something similar a while ago, serving files through php (in my case, not to rename them, but to check that the person downloading them was logged in and was allowed to see those files). I found that pdf downloads didn't work in IE unless I also added these headers:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');

I'm not sure why. Experiment and see what happens.

TRiG