I've searched around google for a way to fopen and read a file with an unknown/random name in a specific folder with a specific extension (it will be the only file in that folder, but it gets updated every hour with a new name), but couldn't find anything. Previously the file had the same name every time unfortunately now it changes...
previously i had to only define it this way:
$file = "/var/uploads/quarter.csv";
$handle = @fopen($file, "r");
but with a random name it doesn't work anymore.
so, I've tried:
function the_file($dir = '/var/uploads/') {
$files = glob($dir . '/*.csv');
$file = $files;
}
$handle = @fopen($file, "r");
script continues...
but it doesn't work. Any help would be appreciated, thanks.
The $file
variable doesn't exist outside the function scope and hence you won't be able to use it outside the function. Also, glob()
returns an array -- if there is only one file, you can just get the first element of the array and return it, like so:
function the_file($dir = '/var/uploads/') {
$files = glob($dir . '/*.csv');
return $files[0]; // return the first filename
}
Now to store it in a variable, you can do:
$file = the_file(); // or specify a directory
# code ...