Baniboy 3 Report post Posted April 4, 2009 So... I need some help with putting up a script. I'm not very good with php...I want php script to do this for me:1. Get all files within a folder.2. Retrieve the 4 first lines from all of them.3. Make and array from the files and an array from the lines in the files and then reverse the arrays.4. Echo the lines from a file and then put a <hr/> tag and then echo from the next file, then another hr tag and then start over etc.I've pretty much figured out how to load specific line(s) from a file: linereader.php<?php$lines = file('file.txt');$l_count = count($lines);for($x = 0; $x< $l_count; $x++){}echo "<font color=red>" . $lines[0] . "</font><br/>";echo "<font color=red>" . $lines[2] . "</font>";?> file.txtechothiscode I might also want it to do more stuff later but for now, all I want is this. Share this post Link to post Share on other sites
jlhaslip 4 Report post Posted April 4, 2009 What are the file extension types that contain the data? Share this post Link to post Share on other sites
Baniboy 3 Report post Posted April 4, 2009 What are the file extension types that contain the data?I need it to retrieve the lines from .txt files. Share this post Link to post Share on other sites
truefusion 3 Report post Posted April 4, 2009 Here's something simple that should do what you want: <?phpforeach (glob("*.txt") as $filename) { $arr = array_reverse(array_slice(file($filename), 0, 4)); foreach ($arr as $value){ echo '<font color="red">'. $value .'</font><br/>'; } echo "<hr/>";}?> Do note i haven't tested this and just wrote it here on the fly, but i don't think there are any errors in it. Also note that depending on the file size of the files and the amount of files it has to go through, this program could use a lot of memory and take a while to load. Share this post Link to post Share on other sites
jlhaslip 4 Report post Posted April 4, 2009 Using the Glob function, read all the lies with a .txt file extension into an array and inside the foreach loop, read the first 4 lines of the file into a buffer.Add the Buffer into an array.Reverse the array, per your request.Print (echo) the multi dimensional array using <hr> tags as dividers between the arrays and not printing blank elements. <?phpecho '<h2>Test of the Text File Read script</h2>';echo '<hr />';echo'<p>Each file contains the same number of lines as its name</p>';echo'<p>For testing purposes, some files contain extra lines</p>';echo'<p>Only the first 4 lines (max) are printed, per the sript request</p>';echo'<p>Blank lines are not printed</p>';echo '<hr />';foreach (glob("*.txt") as $filename) { $handle = fopen($filename, "r"); if ($handle) { for ($i = 1; $i <= 4; $i++) { $buffer[] = fgets($handle, 4096); } $array[] = $buffer; $buffer = ''; fclose($handle); }}$arr = array_reverse($array);foreach ($arr as $arrs) {foreach ($arrs as $key => $value){ if (!empty($value)) { echo "$value <br />"; }}echo '<hr />';}echo '-- 30 -- ';?>Download Sample Files here: http://forums.xisto.com/no_longer_exists/, including this script.Sample files contain the same number of lines as its filename, ie: file 'one' has 1 line and file 'six' has 6 lines.Output is as follows:Test of the Text File Read scriptEach file contains the same number of lines as its nameFor testing purposes, some files contain extra linesOnly the first 4 lines (max) are printed, per the sript requestBlank lines are not printedtwotwo 2threethree 2three 3sixsix 2six 3six 4onefourfour 2four 3four 4fivefive 2five 3five 4-- 30 --{seems to not cut and paste the hr output}Hope this is what you wanted. Post back if you need any more. Share this post Link to post Share on other sites
Baniboy 3 Report post Posted April 5, 2009 (edited) Here's something simple that should do what you want: } echo "<hr/>";}?> linenums:0'><?phpforeach (glob("*.txt") as $filename) { $arr = array_reverse(array_slice(file($filename), 0, 4)); foreach ($arr as $value){ echo '<font color="red">'. $value .'</font><br/>'; } echo "<hr/>";}?>Do note i haven't tested this and just wrote it here on the fly, but i don't think there are any errors in it. Also note that depending on the file size of the files and the amount of files it has to go through, this program could use a lot of memory and take a while to load. That's good, but it has a small problem; the lines in the files are also displayed as reversed. I want them to be printed just as they are in the files, but I want the order of the files to be reversed. The script reads the files in alphabetical order right? My files are gonna be like 1.txt, 2.txt etc. Using the Glob function, read all the lies with a .txt file extension into an array and inside the foreach loop, read the first 4 lines of the file into a buffer.Add the Buffer into an array.Reverse the array, per your request.Print (echo) the multi dimensional array using <hr> tags as dividers between the arrays and not printing blank elements. <?php echo '<h2>Test of the Text File Read script</h2>'; echo '<hr />'; echo'<p>Each file contains the same number of lines as its name</p>'; echo'<p>For testing purposes, some files contain extra lines</p>'; echo'<p>Only the first 4 lines (max) are printed, per the sript request</p>'; echo'<p>Blank lines are not printed</p>'; echo '<hr />'; foreach (glob("*.txt") as $filename) { $handle = fopen($filename, "r"); if ($handle) { for ($i = 1; $i <= 4; $i++) { $buffer[] = fgets($handle, 4096); } $array[] = $buffer; $buffer = ''; fclose($handle); } } $arr = array_reverse($array); foreach ($arr as $arrs) { foreach ($arrs as $key => $value){ if (!empty($value)) { echo "$value <br />"; } } echo '<hr />'; } echo '-- 30 -- '; ?>Download Sample Files here: http://forums.xisto.com/no_longer_exists/, including this script.Sample files contain the same number of lines as its filename, ie: file 'one' has 1 line and file 'six' has 6 lines.Output is as follows:{seems to not cut and paste the hr output}Hope this is what you wanted. Post back if you need any more. This is exactly what I needed! thank you very much! You have NO IDEA of how many problem this thing solved! But I need one more thing (sorry if I'm asking a lot).Is it possible in php to buffer all folders in a folder and then select the last one of them.For example if I have folders 1, 2, 3, 4 and 5. I want it to first NOT BROWSE THE FILES IN THEM (it will be heavy on the server if it browses the files in the folders too right?) but just "look around" and retrieve how many folder there are there. After that, I want it to select the last folder (5) and then execute the code haslip created above for that folder.Is it possible? Thanks again for the code tho, it solved my problem but with this, it will get a lot easier.'Edit: Nevermind, I'm good with this script. Edited April 5, 2009 by baniboy (see edit history) Share this post Link to post Share on other sites