Using PHP to download a file with info from a DB upon form submission?

Okay, I’ve got a Mysqli DB and PHP 7.4. I basically need to store in said database a single file’s name and folder. Then, create an HTML form (in a PHP file) that when submitted, pulls the file’s info from the DB, randomizes its name, and then downloads it to the user’s computer. I’m not very good with PHP, and have no idea how to implement this. I also am not sure how to add the filename and related folder to the DB with correct syntax. If anyone can help me out, I’d really appreciate it.

I’ve been working with a custom snippet of PHP code for a couple years with a DB and all, but this is a new branch I am playing with to allow a user to securely download a file without revealing the file’s actual name or location for an on-line store I am working on.


Well I am not really sure if this the optimal way or not

but you can easily change the output filename with something like this

//$file_orig -> is the path to actual file
//$file_new -> is the new output name

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file_orig));
header('Content-Disposition: attachment; filename=' . $file_new);
header('Content-Transfer-Encoding: binary');


You may also change the content-type depends on your system.

1 Like

Okay, got it working with your code snippet, and it does NOT reveal any path or filenames (as intended)!! Thanks so much, friend!

Sponsor our Newsletter | Privacy Policy | Terms of Service