I’m a bit stuck, i currently have a form that logged in users enter information & submit a file. This uploads to a folder on the server and adds the file name to the database. However when displaying the data in a table i’m stuck on how to download the file based on the ID of the record.
Please can someone advise on how best to do this:-
This is the key bit - change this to use the path of your file from your database row.
$filepath = "uploads/files.htm/" . $file;
If you can’t get it from there, I think you’d need to show your upload code so someone can see how you’ve named the files relative to the row id. Presumably in your download code, all you need to do is open the database, retrieve the row based on the id you pass in via the URL and use $row['file-name'].
Thanks droopsnoot. How would i change this to use the path of the file from the database row inside of download.php?
The files names remain the same of what was uploaded. i.e if the users file is called image.jpg it would upload the filename image.jpg to the table and move the file to files.htm.
As you said, all i’m looking to do is to download a file based on the row / row id
You know the code you posted at the start, which retrieves the row and displays the filename and date created? Use the same code to retrieve it from the database, and then instead of displaying it, put it into the line I highlighted.
And what happens when two users upload something with the same name? I often upload “capture.jpg” to places because that’s what the snipping tool calls a screen grab by default. Even if you have a separate directory for each user, that would cause me a problem in that example.
You’re absolutely right, the next step is to incorporate a file rename on upload. I’ve seen people using MD5 and rand along with time.
What would you suggest is the best way? (and not too difficult to implement as i’m still quite new to php).
Well that’s a good enough way of doing it, but it does rather expose your file storage. As it is only logged-in users that can download files, won’t that make it easy (or at least possible) for users to guess other files?
I’ve no proper experience of it so I wouldn’t like to suggest one. Anything that gives a unique name must be a decent enough way of doing it. Something as simple as using the row id, if it’s unique, would work well enough. You can always store the original image name as well, if it’s important.
A very valid point, i did try as a precautionary measure to access the files through the address bar but i got a 404. Plus i named the file with .htm so that it wouldn’t/shouldn’t appear anywhere. What would you suggest as an alternative?
Thanks Droopsnoot, i’m definitely going to implement this.