Forum Moderators: coopster
I want my script to read a subdirectory containing html files and output the filename and contents of the <title> tag for each file, like so...
foo123.html, "Welcome to Foo"
This is what I've written to output the file names:
<?php
$dir="../subdirectory/";
$fd = opendir($dir);
if($fd) {
while (($filename = readdir($fd)) == true) {
$file_array[]=$filename;
}
sort($file_array);
reset($file_array);
foreach($file_array as $item){
print ($item . "<br>");
}
}
?>
That part works fine.
I want to add this component to extract the <title> tag contents.
if (preg_match("/<title>(.*)<\/title>/", file_get_contents('$item'), $matches)) {
print ($item . $matches);
}
But I can't seem to get it integrated properly into my script. I get an error that says something like "Warning: file_get_contents($item) [function.file-get-contents]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\"
If anyone can help, that would be great!
Thanks!
Same result though. Error msg: "Warning: file_get_contents(filename.html) [function.file-get-contents]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\test\test.php on line 16"
Does this mean that it's looking for the data in the "test" subdir? That's not the subdir I specified in $dir.
If not, what DOES the error msg mean?
Thanks a bunch!
<?php
$dir="../subdirectory/";
$fd = opendir($dir);
if($fd) {
while (($filename = readdir($fd)) == true) {
$file_array[]=$filename;
}
sort($file_array);
reset($file_array);
foreach($file_array as $item){
if (preg_match("/<title>(.*)<\/title>/i", file_get_contents($item), $matches)) {
print ($item . $matches);
}
}
}
$dir->close();
?>
Error msg: "Warning: file_get_contents(filename.html) [function.file-get-contents]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\test\test.php on line 16"Does this mean that it's looking for the data in the "test" subdir?
No, it doesn't. It means that the script test.php has code in line 16 that is looking for some file, namely filename.html, and not finding it.
How about if you try it with an absolute path - relative paths can get confusing sometimes with scripts that are drawing things from various places.
Try this instead
$dir= $_SERVER['DOCUMENT_ROOT'] . "/subdirectory/";
See if that works
$dir = $_SERVER['DOCUMENT_ROOT'] . "/subdirectory/";
...as ergophobe suggested. You should only need:
...file_get_contents($dir.$item)...
in preg_match.
However, $matches gets reset every time through the loop. Plus the title that you want is from the (.*) part of the regex, which I think will be in $matches[1]; To print out the results, you might:
print ($item." - ".$matches[1]."<br>\n");
To save the files and titles in a comparable arrays while you're still in the if condition, you could:
$files[] = $item; // but, set empty arrays before the foreach
$titles[] = $matches[1];
If you want to get titles from dynamic pages, where titling code is included, I'm not sure if file_get_contents will work for you, however. In that case, consider fopen. It's a little clumsier, but it works on URL aware configurations.
I hope this helps.