0

i want to implement code so that when user will download that file, name of the file should be changed

as example

$uploaddir="files/userid/";
$filename=rand(1000,9999).time().rand(1000,9999);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

suppose using this code file is uploaded it will be stored on server as name like this

23451232325654.pdf

but for user he/she will have logical name for it Like learn_php.php

when user want to download this file he/she will have this link to download

www.example.com/files/userid/23451232325654.pdf

but this file not stored on user's pc when downloaded as 23451232325654.pdf but i want to store it as their logical name as shown above learn.php

4

1 回答 1

0

You can do this no problem. You just need a download script that will first send the correct header. In this case, the header should be something like:

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

See example 1 in the php docs.

So instead of linking directly to the file (for example: http://website.com/content/129312.pdf), you would link to your download script (for example: http://website.com/download.php?file=129312.pdf).

And download would first send the headers, then the file contents.

Obligatory note about security: Using the filename directly from $_GET without sanitizing it opens up a huge security issue. If you do it this way you NEED to sanitize it.

于 2012-04-25T07:12:20.163 回答