Reputation: 3211
I have got a button from where users can download specific files. Each time I will have to process the name of the file and put it in a variable before giving it to the readfile function to serve it to the user. However, in many cases the file name is quite long and may change over time so there is a potential of errors in the future. So I was wondering whether one can simply pass something like /filedirectory/*.pdf or just specify only part of the name and rest could be anything and then pass it to the readfile to download the pdf which is found there like in the example below?
if(isset($_POST["submit"]))
{
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.pdf"');
// Whatever name the pdf has
readfile('./filedirectory/*.pdf');
#OR
// File starts with 123..
readfile('./filedirectory/123*.pdf');
}
Upvotes: 0
Views: 83
Reputation: 5393
You can use glob to list all PDF files and then you can select the name that you wish and readfile it.
Example
<?php
foreach (glob("*.pdf") as $filename) {
// Do something with the $filename
}
?>
Upvotes: 1
Reputation: 16
You may get the list of files within /filedirectory using scandir() (http://php.net/manual/en/function.scandir.php) and find out whatever a file name might be.
Upvotes: 0