Jump to content

Read variables from a list of files?????


zrosen88

Recommended Posts

I'm the Assistant Editor for The Charger Bulletin, University of New Haven's college newspaper, and I'm currently designing a website for the paper (the project is temporarily located at http://www.zackrosen.com/zfiles/cbulletin/ )

 

I was wondering if there was a way to do the following and, if so, if you could let me know how!

 

I want a script to read a directory and display the files. -- I know this is possible.

 

However, instead of displaying the filenames, I want the script to display a list of variables.

 

For example:

 

If I had directory /articles/ and every file in that directory had variable $title = "XTitleNameX"; -- I want a list produced that would show each file by title AND by file name (so the resulting list would look similar to:

 

The First Title (page1.php)

Title Number Two (page2.php)

Three's The Charm (page3.php)

 

I hope that makes sense, and that you can help me!

 

Best regards,

 

Zack

Link to comment
https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/
Share on other sites

I'd direct you to the php file handler library best bet is

something like this

<?php
$handle = opendir('/path/to/files')) 
    while (false !== ($file = readdir($handle))) {
        fopen($file,r);
        //Find the var in the file and store it along with file name for later use or echo out now
        fclose($file);
}
?>

 

that ends up giving me these errors:

 

 

Warning: fclose(): supplied argument is not a valid stream resource in [...]index.php on line 6

 

Warning: fclose(): supplied argument is not a valid stream resource in [...]index.php on line 6

 

 

line 6 is:

 

        fclose($file);

I was actually able to do what I wanted like this -- before I start working with it, is there anything WRONG with doing this (I mean, it WORKS!)

 

<?php

if ($handle = opendir('dir/')) {

while (false !== ($file = readdir($handle))) {

if ($file != "." && $file != "..") {

include("dir/$file");

echo "$file - $title<br>";

}

}

closedir($handle);

}

?>

include does mean output if there is output, best method would be to link to them and make some sort of display file, but yeah it works the answer was that $file is an array/handler and thus not the file needed for fclose, it won't matter because all files are closed on the end of php processing, but its good practice to close the file just say $temp = fopen($file) and then fclose($temp) to make sure you don't have flock issues.  But yeah I would just read the file get that var name out of it close it and then display a link to a page designed around displaying the articles. a link to like viewer.php?article=$filename then on that page say fopen($_GET['article']) and manipulate it till its cut to the actual article and display it.  PM me if you got any questions

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.