简体   繁体   中英

how to serve files in a folder with a filename fetched from mysql database using php/.htaccess file?

I have to serve files in a folder without a php script and with another filename that is fetched from DB. example: this is the path to the file: "/app/123456.apk" but it should be provided with the name "example.apk". i mean when i enter "/app/123456.apk" it should be renamed to "/app/example.apk".

You can start with an .htaccess rewrite rule that redirects urls of the form app/*.apk to a getname.php query with the appropriate id. Something like this:

RewriteRule ^app/(.*)\.apk$ /getname.php?id=$1 [L]

Then in getname.php you can get the id with $_GET['id'] , then look that id up in your database to find the filename on disk ( $diskname ) and the filename you want the user to save it as ( $savename );

Given you have that information, you could write out the file with somethig like this:

header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($diskname)); 
header('Content-Disposition: attachment; filename=' . $savename);
readfile($diskname);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM