Reputation: 6052
I have a folder called /attachments
, where I store one csv file. However, that csv file is added dynamically, and therefore I don't know it's exact name. I only know that it will always be a .csv file that will start with "stocklist" (for example: stocklist2141.csv
or stocklist8281.csv
)
How can I "grab" that file using PHP? Right now, I've only been able to find a function for opening a file, where you know the filename:
$filepath = "./includes/api/inventory/attachment/test.csv";
$file = fopen($filepath, "r");
Upvotes: 0
Views: 380
Reputation: 388
As mentioned in a comment, use glob.
The following example retrieves the files with extension .csv in the folder ./files:
//Get all CSV files in folder
$files = glob("./files/*.csv");
foreach($files as $file) {
//Do interesting stuff with the file.
}
Upvotes: 1
Reputation: 6650
$dir = "./includes/api/inventory/attachment/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
Upvotes: 0
Reputation: 3178
Something like this should work:
$files = scandir("./includes/api/inventory/attachment");
if (!empty($files)) {
foreach ($files as $file) {
// do stuff with $file
}
}
Upvotes: 0