List and link php files in a folder and its metatag description

I tried this code, but unsuccessfully:

`<?php  
	echo "<ul>";	  
$phpfiles = glob("*?[!index]?.php");  
$tags = get_meta_tags($phpfiles);  
foreach ($phpfiles as $phpfile){  
    echo '<li><a href="'.$phpfile.'">'.pathinfo($phpfile, PATHINFO_FILENAME).'</a>';  
    echo "<span>";  
    echo $tags['description'];  
    echo "</span></li>";   
}	    
    echo "</ul>";  
?>`  

Where I’m wrong?

What happens when you run this?

I’m not sure this means what you think it means.
So let me ask you: What do you think this means?

(Hint: If I said "*?[!xenid]?.php", i’ve said the same thing you did.)

with $phpfiles = glob("*.php"); I get all php files in a folder, with $phpfiles = glob("*?[!index]?.php"); I should get all php files except index.php: however this code, once working, is no more working. In fact the correct code is

$phpfiles = glob("[^index]*.php");

isn’t it?

My aim is to list all php folder’s files (except index.php) and get them linked and to get the meta tag “description” at the right side of every file.

This code works:

<?php
	   echo "<ul>";	  
$phpfiles = glob("*.php");
foreach ($phpfiles as $phpfile){
     echo '<li><a href="'.$phpfile.'">'.pathinfo($phpfile, PATHINFO_FILENAME).'</a></li>'; 
}	   
	   echo "</ul>";
?>

it list all php files in a folder.

And this works

<?php
$tags = get_meta_tags('my-php-file');
echo "<p><b>description</b>: ";
echo $tags['description'];
echo "</p>";
?>

It show the meta tag “description” of a given, single php file.

So, I trying to merge, so to say, these two codes, but unsuccessfully, for my incompetence :frowning:

What happens, wake689? Warning messages and no meta tag. Here

Warning : get_meta_tags() expects parameter 1 to be a valid path, array given in /mnt/dati/schede/web/web-pro/php-read-metatags.php on line 17

  • php-read-metatags // file correctly linked
    Notice : Trying to access array offset on value of type null in /mnt/dati/schede/web/web-pro/php-read-metatags.php on line 21

I tried with this new code

<?php
	echo "<ul>";	  
$phpfiles = glob("[^index]*.php");
foreach ($phpfiles as $phpfile){
    echo '<li><a href="'.$phpfile.'">'.pathinfo($phpfile, PATHINFO_FILENAME).'</a>';
    echo "<span>";}
foreach ($phpfiles as $files){
    echo "<p><b>$files</b></p>";   //just for testing it
    $tags = (get_meta_tags($files));
    echo $tags['description'];  // line 23
    echo PHP_EOL; // line 24
    echo "</span></li>"; 
}	   
    echo "</ul>";
?>

I get this error message (after correctly linking php files)

config.inc
php-read-exif
php-read-metatags

config.inc.php
Notice: Undefined index: description in /mnt/dati/schede/web/web-pro/php-read-metatags.php on line 24
php-read-exif.php
Notice: Undefined index: description in /mnt/dati/schede/web/web-pro/php-read-metatags.php on line 24
php-read-metatags.php
Notice: Undefined index: description in /mnt/dati/schede/web/web-pro/php-read-metatags.php on line 24

Well, that tells you that get_meta_tags() isn’t returning an element called description. What is in your $tags array when you var_dump() it?

1 Like

Do you mean var_dump($tags);?
Here you are: array(0) { }

OK, so now you need to find out why get_meta_tags() is returning an empty array.

1 Like

OK, but how?

Normal debugging techniques, really. First, is it getting the correct filename and opening it successfully? Second, are there any meta tags in the file? The fact that you get an empty array rather than false suggests it is finding the file correctly, but not finding any tags in it.

How about showing one of the files on here, perhaps someone can see why it isn’t finding the tags?

Thank you. You’re right, there wasn’t metatag “description” beforehand; but now I put it and here is the source content:

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="Description" content="ricavare metatags" />

I omit the whole source code: it is not necessary, is it?

I don’t know. Does it work now?

But, if there were other meta tags, they should have been returned in the array with the “description” tag missing. So your empty array suggests that there is still some problem.

I take it you’ve checked the documentation for get_meta_tags() to see if there’s anything relevant there?

unfortunately, no.

I take it you’ve checked the documentation for get_meta_tags() to see if there’s anything relevant there?

To be honest, I “checked” only threads on Stackoverflow and php online manual, but unsuccessfully, so far.
The most relevant is here. But it doesn’t work.

It’s not the problem this thread’s about at this point, but for the record,

does not mean “anything but index.php”.
^ has no meaning to glob.
! negates a set.
But

does not mean this either.

* = any number of characters
? = exactly 1 character
[!index] = “Any single character that is not i,n,d,e or x.”

*?[!index]?.php = “Any filename that ends in .php, and the rest of the filename is at least 3 characters long, the second-to-last character being not in the set [i,n,d,e,x].”

Yes, that’s what I meant by the documentation. Well, if it’s still not working, maybe time to show one of the files, obviously with any sensitive stuff blocked out. Post the smallest one.

config.inc.php almost certainly doesnt contain meta-tags; a file like that isnt supposed to be HTML…

Are the meta tags you posted above inside the head tag of the file in question?
Also that first meta tag is missing a name attribute. Not sure if that’ll cause problems…

This code works, and it point exactly to one of the three files (above mentioned):

<?php
$url='http://localhost/web/web-pro/php-read-exif.php';
$tags = get_meta_tags($url); 
echo "<p><b>description</b>: ";
echo $tags['description'];
echo "</p>";
?>

The output is correct.
As I said: get_meta_tags works pointing to a single file, not combining with glob.

here you are:

<?php 
$keywords="metatags";
$description="ricavare metatags";
$title="estrarre meta-tags";
$img="http://localhost/img/ritratti/someone.jpg";
include "normal.inc";
include "$root/intell/header-intell.inc";
?>

<h2>solo i tags meta dei files html (/php): get metadata</h2>

<?php
    echo "<ul>";	  
$phpfiles = glob("[^index]*.php");
foreach ($phpfiles as $phpfile){
    echo '<li><a href="'.$phpfile.'">'.pathinfo($phpfile, PATHINFO_FILENAME).'</a>';
    echo "<span>";}
foreach ($phpfiles as $files){
    echo "<p><b>$files</b></p>";   //just for testing it
    $tags = (get_meta_tags($files));
    echo $tags['description'];
    echo PHP_EOL;
    echo "</span></li>"; 
}	   
    echo "</ul>";
 
// the following **works**
$url='http://localhost/web/web-pro/php-read-exif.php';
$tags = get_meta_tags($url); 
echo "<p><b>descrizione</b>: ";
echo $tags['description'];
echo "</p>";
?>  

[some other php code]  

 <?php include "$root/footer.inc"; ?>

One difference there is that you are using a URL for the working code, and a file path for the one that does not. On that basis it would be worth checking that you have the correct path, although I would still have expected that you would get false rather than an empty array if that were the problem.

But you’re not pointing to a single file, you’re pointing to a URL. If you point it to a single file without the http at the start, and with the correct path, does it work as expected?

1 Like

of note, there is no path in glob results; glob isnt recursive. People have over the years suggested/designed recursive glob functions, but the path glob searches is fixed; as such, the found files will be in the current directory. (eventually, RecursiveDirectoryIterator took care of this mostly.)

1 Like