Coverting directory contents into an array to reverse?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Coverting directory contents into an array to reverse?

Post by mesz »

Hello, I use the following ciode to read and display the images from a directory.
I know how to reverse an array when data has been written to a .dat / .txt file, but how do you reverse the array when displaying a directory contents?
( It is not an array - I guess the first step will be to turn teh directory contents into an array - how do I do this? )

Code: Select all

<?php
<?
function CheckExt($filename, $ext) {
$passed = FALSE;
$testExt = "\.".$ext."$";
if (eregi($testExt, $filename)) {
$passed = TRUE;
}
return $passed;
}

//Define an array of common extensions.
$exts = array("gif","jpg$|\\.jpeg","png","bmp");

echo "<b>Images in this folder:</b><br>";
$dir = opendir(".");
$files = readdir($dir);

while (false !== ($files = readdir($dir))) {
foreach ($exts as $value) {
if (CheckExt($files, $value)) {
echo "<img src='$files'><br />";  
$count++; //Keep track of the total number of files.
break; //No need to keep looping if we've got a match.
}
}

}
echo $count." image(s) found in this directory.\n";
?>
<? 
closedir($dir);
?>
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

while (false !== ($files = readdir($dir))) {
foreach ($exts as $value) {
if (CheckExt($files, $value)) {
echo "<img src='$files'><br />";
$count++; //Keep track of the total number of files.
break; //No need to keep looping if we've got a match.
}
}

}
that's the loop displaying the filenames. Now instead of printing all you have to do is to append the values to an array.
$arr[] = ... will append a new element at the end of the array $arr. When the loop's done you can either iterate the array backwards, apply one of the sort-functions (if you like) or do whatever you do with an array returned by file()
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Would these changes do it?

Code: Select all

<?php
function CheckExt($filename, $ext) { 
$passed = FALSE; 
$testExt = "\.".$ext."$"; 
if (eregi($testExt, $filename)) { 
$passed = TRUE; 
} 
return $passed; 
} 

//Define an array of common extensions. 
$exts = array("gif","jpg$|\\.jpeg","png","bmp"); 

echo "<b>Images in this folder:</b><br>"; 
$dir = opendir("."); 
$files = readdir($dir); 

while (false !== ($files = readdir($dir))) { 
foreach ($exts as $value) { 
if (CheckExt($files, $value)) { 

#C H A N G E S

$arr[] = $files
$data = array_reverse($data);
echo "<img src='$files'><br />";  

# E N D O F C H A N G E S

$count++; //Keep track of the total number of files. 

} 
} 
?> 
<? 
closedir($dir); 
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$arr[] = $files
$data = array_reverse($data);
there is no array $data, but $arr is.
Don't reverse the array inside the loop (otherwise each time a filename is appenden the current array is reversed producing a strange ordering)
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Cheers for your help with this Volka - you probably have noticed, beyond simple functions I start to struggle. I think I have it this time...before I go through my incredibly slow FTP process ( behind work firewall ) I just want to ask you if you think it'll work?

Code: Select all

<?php
function CheckExt($filename, $ext) { 
$passed = FALSE; 
$testExt = "\.".$ext."$"; 
if (eregi($testExt, $filename)) { 
$passed = TRUE; 
} 
return $passed; 
} 

//Define an array of common extensions. 
$exts = array("gif","jpg$|\\.jpeg","png","bmp"); 

echo "<b>Images in this folder:</b><br>"; 
$dir = opendir("."); 
$files = readdir($dir); 

while (false !== ($files = readdir($dir))) { 
foreach ($exts as $value) { 
if (CheckExt($files, $value)) {

$count++; //Keep track of the total number of files. 
} 
} 

# changes

$arr[] = array_reverse($files); 
echo "<img src='$files'><br />";  

# end of changes

?> 
<? 
closedir($dir); 
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

nope.
Within the loop (when the filename passed your test) you want to append something to an existing array.
Outside the loop you want the resulting array to be in reverse order.
What you have now is:
after the loop is done (and $file therefor is something evaluating to false) passing the value of $files to the function array_reverse() and appending the return value of this function to the array $arr.
you probably have noticed, beyond simple functions I start to struggle
I certainly did ;)
But this is not a "write me a script" forum and we encourage beginners not to simply copy&paste code snipptes but to learn php.
Last edited by volka on Thu Oct 23, 2003 9:11 am, edited 1 time in total.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

I've tried it!
Doesn't work... :x
any pointers?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

  • as long as there are filenames left
    • if the current filename passes the test
      • append that filename to the array
  • after the loop's done reverse the array of all fitting filenames
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Is this it?

Code: Select all

<?php
<?php 
function CheckExt($filename, $ext) { 
$passed = FALSE; 
$testExt = "\.".$ext."$"; 
if (eregi($testExt, $filename)) { 
$passed = TRUE; 
} 
return $passed; 
} 

//Define an array of common extensions. 
$exts = array("gif","jpg$|\\.jpeg","png","bmp"); 
$dir = opendir("."); 
$files = readdir($dir); 
while (false !== ($files = readdir($dir))) { 
foreach ($exts as $value) { 
if (CheckExt($files, $value)) { 

$count++; //Keep track of the total number of files. 
$data = file('$filename'. a+); 
} 
}

$data = array_reverse($data); 
echo "<img src='$files'><br />";  
?> 
<? 
closedir($dir); 
?> 
?>
I think I have fulfilled all the criteria.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're getting closer but stranger errors appear within the code
Try to explain your changes to me, please
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

I guess you want the explanation because you don't want some lazy person visiting you and getting a free script! - I don't blame you at all.
I first used the unreversed array about 6 months ago

My changes....
well let me begin....

Code: Select all

<?php
function CheckExt($filename, $ext) { 
$passed = FALSE; 
$testExt = "\.".$ext."$"; 
if (eregi($testExt, $filename)) { 
$passed = TRUE; 
} 
return $passed; 
} 

//Define an array of common extensions. 
$exts = array("gif","jpg$|\\.jpeg","png","bmp"); 
$dir = opendir("."); 
$files = readdir($dir); 
while (false !== ($files = readdir($dir))) { 
foreach ($exts as $value) { 
if (CheckExt($files, $value)) { 

$count++; //Keep track of the total number of files. 
$data = file('$filename'. a+); 
} 
} 

$data = array_reverse($data); 
echo "<img src='$files'><br />";  
?> 
<? 
closedir($dir); 
?>
>>>>>>>>>>>>>>>>>>
$count++; //Keep track of the total number of files.
$data = file('$filename'. a+);
After defining the type of files wanted I am creating an array called $data to append them to.
}
}

$data = array_reverse($data);
echo "<img src='$files'><br />";
After the loop has finished I reversed the array and asked for the files to be printed in reverse order.

However I now suspect it should be
echo "<img src='$data'>; [/quote]

Or do I have to explode the $data array into seperate elements?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hm, this is probably getting us nowhere.
no offense but I suggest you start reading the first few chapters of the php manual at http://php.net/manual to get a basic understanding of what you're doing (I doubt you have ;) )

Then try to fill in the scheme:
That's the script you've posted first

Code: Select all

- Define an array of common extensions.
- open the current directory and assign the resource returned to $dir

- as long as the return value of readdir (assigned to $files) is unequal false
|	- iterate all elements of the array of common extensions
|	|	- if CheckExt return true for $files and $value
|	|	|	- echo the filename and increase the counter
now you don't want to display the filename immediatly but to store it in an array

Code: Select all

- Define an array of common extensions.
- open the current directory and assign the resource returned to $dir
- $files = readdir($dir); // that's skipping the first filename, why ever
- make an emtpy array called $filenames

- as long as the return value of readdir (assigned to $files) is unequal false
|	- iterate all elements of the array of common extensions
|	|	- if CheckExt return true for the current filename and current extension
|	|	|	- append the filename to the array $filenames

-reverse the array $filenames
- iterate all elements of the array $filenames
|	- display the current filename
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

no offense taken - I have the best intention of being thorough, but thsi development thing is a side line to design and a thousand other obligations at present.
Maybe this'll urge me onto a new level of thoroughness!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, i c. Designers should code and coders should design :]

Code: Select all

<?php
function CheckExt($filename, $ext) {
	$passed = FALSE;
	$testExt = "\.".$ext."$";
	if (eregi($testExt, $filename)) {
		$passed = TRUE;
		break;
	}
	return $passed;
}


// - Define an array of common extensions.
$exts = array("gif","jpg$|\\.jpeg","png","bmp");
// - open the current directory and assign the resource returned to $dir
$dir = opendir(".");
// -make an emtpy array called $filenames
$filenames = array();
// - as long as the return value of readdir (assigned to $files) is unequal false
while (false !== ($files = readdir($dir))) {
	// - iterate all elements of the array of common extensions
	foreach ($exts as $value) {
		// - if CheckExt return true for the current filename and current extension
		if (CheckExt($files, $value)) {
			// - append the filename to the array $filenames
			$filenames[] = $files;
			break; //No need to keep looping if we've got a match.
		}
	}
}
closedir($dir);

// -reverse the array $filenames
$filenames = array_reverse($filenames);


echo '<b>Images in this folder:</b><br />';
// - iterate all elements of the array $filenames
foreach($filenames as $fn)
{
	// - display the current filename
	echo "<img src='$fn'><br />";
}
echo count($filenames), ' image(s) found in this directory.';
?>
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

I really appreciate your help with this.
I have one error though -
Fatal error: Cannot break/continue 1 level in /home/virtual/site140/fst/var/www/html/pic/images/view2.php on line 7
If it will help with your sympathy further, I agreed to build a quick site for a work colleague and every day the list of requirements grows - and despite my skills ( lack thereof ) I'm overambitios, and a perfectionist!
Post Reply