• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

download file with a different name.. how ?

hi all thank you for your help in advance
well, i have made  a code which the user  can upload a file and  the script changes the file name into a random number plus some thing like date and time to prevent repeation in the server , so  the file name comes something like this :
4778632-11042009-105236.pdf

the problem is that when the user comes to download the file he downloads it with the same long name, Can any one help how to make a default name that i can set to make people download the file without the real name on the server.

regards
0
eCamel
Asked:
eCamel
  • 2
2 Solutions
 
iserkoCommented:
You will need to serve the files using PHP.

I presume you save the original filenames in a database? If so, its only a matter of reading the original filename from the database, reading the file from the disk and serving it.

Example:


<?php
 
// Get original filename from dB
$orig_filename = get_orig_filename_from_db();
// Get the path to the actual file
$path_to_file = get_path_from_db();
 
 
// If this part doesn't work, use the alternative
// =================
$finfo = finfo_open(FILEINFO_MIME, "/usr/share/misc/magic"); // This path depends on your distribution
if (!$finfo) {
    echo "Opening fileinfo database failed";
    exit();
}
$file_mimetype = finfo_file($finfo, $path_to_file);
// =================
// == Alternative ==
// $file_mimetype = mime_content_type($path_to_file);
// =================
 
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_mimetype);
header('Content-Length: ' . filesize($path_to_file));
 
?>

Open in new window

0
 
iserkoCommented:
Forgot about this (add it after content-length):
header('Content-Disposition: attachment; filename=' . $orig_filename);

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now