Solved

download file with a different name.. how ?

Posted on 2009-04-12
2
223 Views
Last Modified: 2013-12-12
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
Comment
Question by:eCamel
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
iserko earned 500 total points
Comment Utility
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
 
LVL 3

Assisted Solution

by:iserko
iserko earned 500 total points
Comment Utility
Forgot about this (add it after content-length):
header('Content-Disposition: attachment; filename=' . $orig_filename);

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now