PHP newbie - dealing with a bit complex file handling

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

Post Reply
rudi feijo
Forum Newbie
Posts: 9
Joined: Tue Jun 20, 2006 1:00 pm

PHP newbie - dealing with a bit complex file handling

Post by rudi feijo »

I realized this post is rather huge so to make it simple:

I have about 1300 files following this naming structure
Still + "artist name" + "image index" + "a constant tag" + ".cfi"
examples:
StillAAndrewGonzalez000(http://www.caiaffa.com).cfi
StillLopesGarcia013(http://www.caiaffa.com).cfi

I have to read the directory files, and assign and array wich will hold the artists name and the numer of images that
the artist have in the directory. this is determined by the highest number found in the image index part of the filename

I am able to read the directory, and make and array with the artists names, but i need to assign the number of images value
in the same reading process as I am using to split the artists name apart.

-----------------------
-----------------------



Hello

I am creating a web-based images gallery slideshow wich needs a lot of unic features associated to it.

The script must be able to do a few things:

-Capture the image files from the web directory in the array (done)

-Recognize a certain string in the file name, wich will be used to associate files to its artists:
-all images files follow the same naming structure, wich is:
Still + "artist name" + "image index" + "a constant tag" + ".cfi"
the cfi extension is just any image (jpg,bmp,jpeg) renamed to cfi
examples:
StillAAndrewGonzalez000(http://www.caiaffa.com).cfi
StillLopesGarcia013(http://www.caiaffa.com).cfi

-This slideshow php script should pick a random artist from the entire list of artists, then select n images
from the selected artist for playing. When the playback is done, it repicks another artists and images.

-It must have conditions to avoid repeating artists that were already played, untill all artists have been played.
Also, avoiding to select the same instance of images when selecting the n images from the current artist.

I have the basic slideshow code running at
http://www.caiaffa.com/Showcase/testeFADE8.php
So far, all it does is agregate the image files in an array and using it to play files randomly.


---
I have coded the entire solution in flash-actionscript,
http://www.caiaffa.com/Showcase/Galeria ... %20net.swf
wich is much more my game, but unfourtunately the project
now demands that I code it entirely in php/js/html, all of wich Im very novice.

the actionscript solution used a manually created txt list of all the artists and how many images each of them have in the
directory, now I should work towards coding it so that it will generate this listing dinamically.


Any help is appreciated, even if only to direct me to some usefull tutorials.



It is the string manipulation part wich im battling with. I should be able to make all the array assigning in one reading of the directory files, but its been hard to figure out the correct way.

From my images of directory i need to:
-read each file name
-take out the "Still" string AND (http://www.caiaffa.com).cfi AND the three numbers before the caiaffa url tag
-assign the remaining as the artist name
-read the image index (the three numbers) and, untill the artist name doesnt match anymore, sum it all up and assign the
value as the total number of images for that artist.

so, for StillAAndrewGonzalez000(http://www.caiaffa.com).cfi
it would take the artist name (AAndrewGonzalez), put it in the artists array
then for every new file with the same artists name, add up to an image counter
after reading all files is done, assign the image counter numbers to their respective artists, in a new array or in a possible
multidimensional array storing Artist and Number of images.

the way im working right now, im trying to match "000" to the filename string, and if theres a match, split and store the artist name.
this is ok to hold the artist name, but if i want to assign how many images any artist have, ill have to re-read the entire filenames
array.

what i got so far (wich will end up wastefull)

Code: Select all

<?

function returnimages($dirname="./Galeria/") {
	$pattern="(\.jpg$)|(\.cfi$)|(\.jpeg$)|(\.gif$)";
	$defaultdir="http://www.caiaffa.com/Showcase/Galeria/";
	$files = array();
	$curimage=0;
	$fadeimages= array();
	$artists= array();
	if($handle = opendir($dirname)) {
		while(false !== ($file = readdir($handle))){
		if(eregi($pattern, $file)){
			array_push ($fadeimages, $defaultdir . $file);
			print_r($fadeimages[$curimage]);
			$curimage++;
			echo '<br>';
			$pos = strpos($file, "000");
			if ($pos === false) {
				} else {
					$chunks = split ("000", $file, 5);
					$finalchunk = split ("Still", $chunks[0], 5);
					array_push($artists ,$finalchunk[1]);
					print_r($finalchunk[1]);
					    echo '<br>';
				}
			}
		}
		closedir($handle);
	}
	echo '<br>';
	echo '<br>';
	echo '<br>';
	print_r($artists);
	return($files);
}
returnimages();

?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

If you do some type of thing that matches if the number is between 000 and 999 using

Code: Select all

range(000, 999)
where you have

Code: Select all

$chunks = split ("000", $file, 5);
Because right now its only looking for a 000 not another number. if I am thinking right?

I'm not 100% sure what to do so if anyone eles could help rudi feijo....

PS. Why do you need the number?

ADDED:
PHP Manual wrote:Tip: preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.
You might be able to do

Code: Select all

preg_split("^[0-999]$", $file, 5);
rudi feijo
Forum Newbie
Posts: 9
Joined: Tue Jun 20, 2006 1:00 pm

Post by rudi feijo »

Thanks for your help

I need the number for the slideshow program to know how many images each artist have in total.

I need this because if an artist have 100 images, he will be treated differently then an artist who have only, say, 5 images.

Since i cannot have repeating ocurrances of the same work of art image, if I tell my program to play a set of ten files, from an artist who have only 5, it should automatically modify the playlist set to play only 5 images instead of the specified 10.

But. now that you actually brought up the question, im wondering if I can just do without this number.

The final slideshow program will have instructions like "artist jack, play 10 images;artist joe, play 20 images;artist mary, play 7 images" and so on.
The "showcase" slideshow wich is what im coding now will consist of a constant number of images to be played from each artist, I think it will be 5 images --> select an artist, play 5 images from him, move on to the next artist.

---

Maybe I could just search for the images related to the artists directly at run time without having to specify how many images each have, and then automatically cap the total-images-to-be-played if necessary if there wasnt enough matches found.

The way I was gonna code it, wich is how Ive done in actionscript, is to pick a random artist from the array, pick a random number from the total image number of that artist, then construct the filename of the image to be played.

----

the reason why i used "000" is because since each artist will only have one image with a 000 index, it seemed like a smart way to agregate the artists array. and this way even an artist with only one image in the directory (wich will have to be 000 by my naming standard), will be included in the artists array
Post Reply