0

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

qvotaxon
  • 370
  • 5
  • 16

1 Answers1

1

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);
Ben Harris
  • 547
  • 3
  • 10
  • I tried your solution, and it works. But I have to add $_SERVER['DOCUMENT_ROOT'] . "/../" before the filename. Is there any way you know of, of searching from the actual root. Not the public file directory set in apache? I'm really noobish at file edits and searches in PHP. Never really done much with it to be honest. – qvotaxon Jan 31 '18 at 17:16
  • I found the answer. I forgot to mention I'm using Laravel. Laravel has helper functions for this purpose. I use the base_path() function to get the project root directory. Many thanks for your help! – qvotaxon Jan 31 '18 at 17:23
  • Missed your reply. Glad you found the answer! Another one to remember is the `__DIR__` magic constant that will populate with the directory that the script is run from. – Ben Harris Jan 31 '18 at 20:04