JasonDavis
JasonDavis

Reputation: 48933

Rename a file with PHP right before it downloads without saving it to the server?

I have an Adobe Illustrator file (AI) that we currently have a link to on a website which then downloads the file to your computer.

The link looks something like this...
http://domain.com/crm/index.php?entryPoint=fileupload_download&id=22440435-e8ee-bd6f-7612-533b2cd7690f&field=fuaifile_c&type=D1_Designs

What I need to do is rename this file as it downloads.

So I am asking if it is possible to pass this download through another PHP file right before it downloads which would allow me to change the filename on the fly that the user downloads. I cannot change the filename on the server but when it downloads I would like to be able to add some ID numbers to the filename on the fly if this is possibble? Any ideas how to accomplish this without having to resave the image on the server with a new name?

Upvotes: 1

Views: 2294

Answers (3)

Marc B
Marc B

Reputation: 360672

It's ugly, and assumes these aren't "large" files that would exceed your memory_limit, but

$data = file_get_contents($original_url);
header('Content-disposition: attachment; filename="new name with id numbers');
header("Content-type: application/octet-stream");
echo $data;

You could always enhance this to do byte serving - suck 10k from original url, spit out 10k to user, etc...

Upvotes: 3

Jim
Jim

Reputation: 22656

Just set the Content-Disposition:

header('Content-Disposition: attachment; filename="downloaded.pdf"');

(Example taken from PHP docs: http://www.php.net/manual/en/function.header.php).

Adding id:

$id = generateIdFromSomewhere();
header('Content-Disposition: attachment; filename="downloaded'.$id.'.pdf"');

Upvotes: 2

user149341
user149341

Reputation:

What you are looking for is the Content-Disposition header, as specified in RFC 2183:

Content-Disposition: attachment; filename=example.ai

You can set this header using the PHP header() function.

Upvotes: 5

Related Questions