mmeister3 Posted June 7, 2011 Share Posted June 7, 2011 I started taking a web development class and im having trouble running a loop that opens multiple files. The files are label as so: movie_review0.txt movie_review1.txt movie_review2.txt movie_review3.txt movie_review4.txt We have to write a loop not knowing how many files there are in the folder. Does anyone have any suggestions? I've tried using a while loop but I dont think I have the syntax correct. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/ Share on other sites More sharing options...
AbraCadaver Posted June 7, 2011 Share Posted June 7, 2011 What did they say in the class? I find it hard to believe that you have an assignment before they have taught you anything. Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226671 Share on other sites More sharing options...
Maq Posted June 7, 2011 Share Posted June 7, 2011 Look at: http://us2.php.net/glob Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226673 Share on other sites More sharing options...
mmeister3 Posted June 7, 2011 Author Share Posted June 7, 2011 Look at: http://us2.php.net/glob We went over it in class but it was a few weeks ago. would something like this work? $reviews = glob('review') Would this output an array of file names that I could index from? Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226674 Share on other sites More sharing options...
Maq Posted June 7, 2011 Share Posted June 7, 2011 Example #1 Convenient way how glob() can replace opendir() and friends. foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226680 Share on other sites More sharing options...
mmeister3 Posted June 7, 2011 Author Share Posted June 7, 2011 So I'm getting frustrated and tired and I know this is simple but I cant figure out how to do it. I have this array: $files = Array ( [0] => tron_info.txt [1] => tron_review0.txt [2] => tron_review1.txt [3] => tron_review2.txt [4] => tron_review3.txt [5] => tron_review4.txt [6] => tron_review5.txt [7] => tron_review6.txt ) Now I need to remove anything that doesnt contain the word 'review' I know this has to be simple but at this point im about to break my computer in half. Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226726 Share on other sites More sharing options...
proning Posted June 7, 2011 Share Posted June 7, 2011 http://www.php.net/manual/en/function.preg-match.php maybe this will help Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226748 Share on other sites More sharing options...
kenrbnsn Posted June 8, 2011 Share Posted June 8, 2011 You need to know something about wild-card filename matching in Linux (Unix). I have these files in my directory: tron_movie.txt tron_review0.txt tron_review1.txt tron_review2.txt tron_review3.txt tron_review4.txt Using this code <?php $x = glob('*review*.txt'); echo '<pre>' . print_r($x,true) . '</pre>'; ?> gets me Array ( [0] => tron_review0.txt [1] => tron_review1.txt [2] => tron_review2.txt [3] => tron_review3.txt [4] => tron_review4.txt ) Ken Quote Link to comment https://forums.phpfreaks.com/topic/238716-opening-multiple-files-in-loop-using-php/#findComment-1226797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.