How do I open documents (.txt, .doc, .pdf, etc.) through their links that I have retrieved in a browser?

Hi!

I'm working on a basic document management system using PHP / MYSQL.

I have the files uploaded to a folder on the server. I have also added a reference to each file in a mySql database at the time of upload. Through a web browser, I can successfully display the filename as a link to the file in the folder (that I had uploaded the files into which I have named "uploaded_files").

My PROBLEM is that when I click the link (filename) it takes me to the "uploaded_files" folder through the browser AND DOES NOT OPEN THE FILE.

WHAT I REQUIRE: is that when I click the filename link (for example: name.txt) It should open the file through the associated application on the Operating System. HOW DO I DO THAT?

It works but not in the way that I require and I'm sure it requires some tweaks!
when I click on the filename link, it takes me to the directory of my webserver , and from there, when I select and click the link, it opens.

I need it to avoid going to the folder on my webserver- obviously and for so many reasons...

Please could I request you to have a look at the code snippet that I have attached to see what I have done wrong- I HAVE DONE SOMETHING WRONG!!!! Can you please help me????
Thanks a lot.

Thanks a lot adbrett for your solution. I've been trying to implement it to work but have not yet been successful. I'm still trying. I have not noticed the advantage or difference of placing the readfile() code "Right at the top" .
I still am not getting the file to open without going through the directory on the server and AGAIN SELECTING / CLICKING THE REQUIRED FILE.
I'm sorry but I have to say that I have tried but am not able to understand or implement the attached line either as using it makes no noticeable difference that I can see. Thanks a lot for the time being.

1) The read file code should be at the top because it is sending browser headers which should be before all other output to work

2) This is required for the soltuion to work as you want. All you are doing your way is linking to the file, and not even correctly, which is why it just links to the directory.

My way is passing the filename as a variable back to the script and the script is forcing a download - which is what you want to do.

If you implement the soltuion as I posted it will work as you want, if you don't without a better reason than "i don't want to" I can't help you further.

0

786aslamkhanAuthor Commented: 2010-04-03

Hi adbrett!
First of all please excuse me for not responding earlier as I was kept from doing so at work- I'm very sorry for the delayed response.
I think that I have unwittingly given you the wrong impression for which too, I profusely apologize.
The wrong impression is that I do not want to implement the solution as you posted... In fact I'm trying to learn from you and I have been trying to implement the code and have somehow not been able to accomplish what is desired.
Thank you for the excellent explanation of placing the readfile() code at the very top.... passing the headers.... yes that does indeed force the open / download dialog box (without going to the folder on my server)-
I think that I may have some path issue (which I am exhausted trying to solve) as when I click the open / download button ,,, I get the download dialog with the correct application selected but the download shows 0 kb. The file is not present in the temp file of my browser where it had downloaded neither is it saved where I specified it to be.
I have attached the code for your perusal. I have been trying to implement it just in the way you have advised.
Thanks for the explanation once again and the solution... please do help me and see where I have gone wrong.

If you see lines 5, 11, and 14, you are using the variable $file which hasn't been set yet - that may be my fault for not including it in my original solution, sorry. This means you are trying to get the filename of an empty string (which doesn't exist), then the filesize (which is 0), and then calling readfile to get it's contents (which is nothing).