Home > OS >  PHP - glob() returns no result when using $variable
PHP - glob() returns no result when using $variable

Time:10-08

This shows the texts to check whether it matches the format inside the curly brackets, and it does.

$file = file_get_contents("C:/xampp/htdocs/gomanga/data/account/Getto/favorites/favorites.txt");
       echo $file; // shows the data: Update here,Gez,Box,Corn,Asero

Here, putting the data manually shows my desired output.

$dir = glob("C:/xampp/htdocs/gomanga/central/{Update here,Gez,Box,Corn,Asero}", GLOB_BRACE);
foreach($dir as $d){
  echo $d.'<br>';
}

But this doesnt work, $file has the same data as the previous

$dire = glob("C:/xampp/htdocs/gomanga/central/{$file}", GLOB_BRACE);
print_r($f); //returns no result just Array ()
foreach($dire as $f){
  echo $f.'<br>'; //no result
  
}

favorites.txt will be updated by the users so I want the above code to work. Maybe I'm reading the txt file wrong?

CodePudding user response:

When using the following format...

"C:/xampp/htdocs/gomanga/central/{$file}"

the braces are used to indicate to PHP that you are using a variable substitution (Curly braces in string in PHP). This means the result won't have the {} part in it.

To make sure they stay in the string, you can just double up on them....

"C:/xampp/htdocs/gomanga/central/{{$file}}"
  • Related