Jump to content
xisto Community
Baniboy

Complex Php Script. I need a script for my site...

Recommended Posts

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

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

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 script
Each file contains the same number of lines as its name

For testing purposes, some files contain extra lines

Only the first 4 lines (max) are printed, per the sript request

Blank lines are not printed
two
two 2
three
three 2
three 3
six
six 2
six 3
six 4
one
four
four 2
four 3
four 4
five
five 2
five 3
five 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

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 by baniboy (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.