zrosen88 Posted July 7, 2007 Share Posted July 7, 2007 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 More sharing options...
cooldude832 Posted July 7, 2007 Share Posted July 7, 2007 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); } ?> Link to comment https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/#findComment-292184 Share on other sites More sharing options...
zrosen88 Posted July 8, 2007 Author Share Posted July 8, 2007 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); Link to comment https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/#findComment-292207 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 yeah it will fail because its not the file but the resouce i g2g but someone can help u Link to comment https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/#findComment-292209 Share on other sites More sharing options...
zrosen88 Posted July 8, 2007 Author Share Posted July 8, 2007 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); } ?> Link to comment https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/#findComment-292223 Share on other sites More sharing options...
JP128 Posted July 8, 2007 Share Posted July 8, 2007 I thought that when its included it outputs everything on the page... It doesn't do that? or is it all php? (Meaning now HTML) Link to comment https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/#findComment-292225 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 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 Link to comment https://forums.phpfreaks.com/topic/58887-read-variables-from-a-list-of-files/#findComment-292254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.