Creating link to file or temp file for user download

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am designing a document management system (DMS). The files are stored
in a directory on the webserver. The filenames for the documents are
stored in a MySQL database.  The DMS will track revisions from the
initial version through the updates. We must have strict permissions
set so only authorized personnel can access some documents. All of the
documents when uploaded are given totally random names such as
"114723fb5422c6bd5ed18f6.doc". Is there a way, without the webserver
creating a new file name "document_specs_ver2.doc" to have the file
download named "document_specs_ver2.doc".

If I must physically create a new file, how do I know when the
webserver would be allowed to delete it? Notes: The files will be in
several different file formats, not just "doc".

Thanks in Advance,


Re: Creating link to file or temp file for user download

*** dspohn wrote/escribió (20 Jun 2005 09:42:24 -0700):
Quoted text here. Click to load it

Sure. Rather than linking to files, link to a download script. This example
is for PDF files and $folder is a directory outside the web server root,
but it can be easily modified for different usages:


header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . $file . ';');
header('Content-Length: ' . filesize($folder . '/' . $file));

readfile($folder . '/' . $file);

-- Álvaro G. Vicario - Burgos, Spain
-- - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group

Re: Creating link to file or temp file for user download

Thanks, thats perfect

Re: Creating link to file or temp file for user download

A tada dspohn rece:

you send the apropriate headers and then stream (just echo it) the content of
that file
here's some sample code for generating CSV files under ASP (I know you want PHP
but .... :-)
hope I was clear enough


   response.ContentType = "Application/"
   response.AddHeader "Content-Disposition", "inline; filename=pegla_mass_mail-"
& second(now) & ".csv"
// above two lines are the headers which define the filename and the browser
will ask you where to save the file. it won't show it in window
// and then we jut echo the whole file or build it line by line as I did here
broj;Drzava;Spol_muski;" & vbCrLf)
   Do While not conn.eof
    response.Write("""" & conn("Alias") & """;")
    response.Write("""" & conn("Email") & """;")
    response.Write("""" & conn("Ime") & """;")
    response.Write("""" & conn("Prezime") & """;")
    response.Write("""" & conn("Adresa") & """;")
    response.Write("""" & conn("Grad") & """;")
    response.Write("""" & conn("Postanski_broj") & """;")
    response.Write("""" & conn("Drzava") & """;")
    response.Write("""" & conn("Spol_muski") & """;")
  End If

Re: Creating link to file or temp file for user download

dspohn wrote:
Quoted text here. Click to load it

Alternatively, if you're running under Apache, you can use mod_rewrite
and .htaccess.  mod_rewrite can pick up the file named
"document_specs_ver2.doc" and instead download
"114723fb5422c6bd5ed18f6.doc", for instance.  .htaccess can also control
access to the files and directories.

You'd have to manage the .htaccess file - but if the data doesn't change
that much, it's pretty easy.

You might also look at a mod_rewrite for mysql - I don't know if one
exists or not.  However, there is a mod_auth_mysql on to
handle the authorizations.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline