0

I'm currently working on a script (in PHP, but I'm not sure if that's relevant here) which allows users to set up a simple file-sharing system on their server. I'm stuck when trying to figure out how to deal with file downloads.

I see two possible approaches here:

1. Direct link to files on the server

Such as https://example.com/files/file.txt

2. Use a script to fetch files

Such as https://example.com/fetch.php?file=file.txt
(or alternatively creating a unique fileId which is linked to a filename in a database, and using that to fetch the file)

Which one should I use? What are the (dis)advantages of each system?

0

3 Answers 3

4

Both approaches have advantages and disadvantages, you need to weigh them according to your requirements. Direct links can be faster or use less server resources, may even scale better if you use a content delivery network but give you little control over who actually accesses them. Links via a server script might give you better control over access rights but may incur performance penalties. A mixed approach (making the file available as a direct link under a unique name that goes away after the user downloaded it) may be an option, too, and is not too hard to implement but doesn't work with a CDN.

1
  • Most CDN's should allow you to cache any sort of GET request. It's the POST requests that aren't cacheable. Furthermore you can have a "real" looking file path in a URL which is then interpreted by a PHP script, which then loads the file from its actual location on disk. Commented Aug 14, 2018 at 11:42
1

You don't have to use the query syntax to use the script. Even you don't make any additional configuration, merely accessing https://example.com/fetch.php/file.txt url would call the script, providing you the file path as a request parameter.

Further, you can set up request rewriting (apache, iis, example) to do whatever transformation, isolating the URLs users are accessing from how they are really handled. So, you can serve your option 1's urls with script as well.

1
  • 1
    The question wasn't about URLs. This still doesn't help me decide whether or not I should link files directly or fetch them through a script Commented Aug 13, 2018 at 3:57
0

Unless you have a strong reason otherwise, prefer using a direct file link.

It will be more efficient, secure, and easier to get correct.

There ARE reasons to use the script approach: - to add special security / header checking - to load the file from a special place (like a database)

But the builtin (to your webserver) downloading will be more performant than anything you are likely able to write directly in php code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.