Search code examples
phpfile-get-contentsglob

Searching for file in directory using glob, not returning results


I'm trying to make a small script for retrieving configuration files containing mapping instructions for a product import, using php glob. But I fail miserably.

What I have right now:

echo "Shop type: " . $this->shopType . '<br />';
echo "Shop version: " . $this->shopVersion . '<br />';
echo "Glob search: config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";

foreach (glob("config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
exit;

$this->shopType represents the name of the shop from which the export file should be imported. E.g. ccvshop.
$this->shopVersion represents the version of the shop, to be able to write different import mappings for different versions of the shop types.

The value of shopType = ccvshop.
The value of shopVersion = 11.2.

Which would make the search string in the glob function: config/mappings/ccvshop_11.2.php.

Unfortunately my result is empty, and I can't see what I'm doing wrong.

Any help would be appreciated.

I added an image representing my directory structure: Directory structure


Solution

  • glob() returns an array of matching files. It looks like you already know the name of the file that you want to read. Try something like this:

    $filename = "config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
    if (file_exists($filename))
        echo "$filename:" . filesize($filename);