Google Answers Logo
View Question
 
Q: PHP Script that counts JPEGs in a directory and returns result to Flash ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: PHP Script that counts JPEGs in a directory and returns result to Flash
Category: Computers > Programming
Asked by: jugglerpm-ga
List Price: $25.00
Posted: 18 May 2005 10:22 PDT
Expires: 17 Jun 2005 10:22 PDT
Question ID: 522972
I would like ActionScript 2.0 and PHP code that count the number of
JPEGs in a directory and return the resulting answer to Flash to be
used as an ActionScript Variable.

The ActionScript code should call the PHP code to ask it to count the
files, then the PHP script should count the files and return the
answer to ActionScript where it can be output as a text variable to
show the script is working.

My plans for the code:
I want to use this code to create a photo gallery that is easy to
maintain and update with fresh images. If my SWF file can know how
many JPEGs there are in a directory I won't have to edit the SWF file
every time I add or delete files from the directory as long as I
follow a naming convention for the JPEGs.
Answer  
Subject: Re: PHP Script that counts JPEGs in a directory and returns result to Flash
Answered By: wildeeo-ga on 19 May 2005 20:16 PDT
Rated:5 out of 5 stars
 
Hi, jugglerpm. Thanks for your question.

Flash and PHP make this task fairly simple. Using a short PHP script
and some simple Actionscript, you can get the number of files with a
specific extension in a directory.


The PHP script would have to contain:


<?

$imageDir = "/path/to/your/images";

$fileCount = 0;

if (is_dir($imageDir) && $directoryPointer = @opendir($imageDir)) {
	while ($oneFile = readdir($directoryPointer)) {
		$thisFileType = strtolower(substr(strrchr($oneFile, "."), 1));
		if ($thisFileType == "jpg" || $thisFileType == "jpeg") {
			$fileCount++;
		}
	}
} else {
	$fileCount = -1;
}

echo $fileCount;

?>



This script will return a single number giving the number of files in
the directory $imageDir (specified at the top) which end in either
".jpg" or ".jpeg".


You can get the value the script returns using a piece of actionscript like:


varObject = new LoadVars();

varObject.onLoad = function () {}
varObject.onData = function (dataIn) {
	numberOfFiles = unescape(dataIn);
}

varObject.load("filecount.php");


The 'onData' method is called when the php script has finished. The
variable numberOfFiles will contain the number the PHP script returns,
which should be the number of images.


If you have any questions, please request a clarification.

--wildeeo
jugglerpm-ga rated this answer:5 out of 5 stars
Hi Wildeeo, Thanks so much. Exactly what I wanted. -pm

Comments  
Subject: Re: PHP Script that counts JPEGs in a directory and returns result to Flash
From: mohsendevelop-ga on 19 May 2005 06:02 PDT
 
It's very simple to this man !
Try to script a PHP that counts the JPG files in a directory you want
it's very simple It has it's own function to this for you.
After that create an instance of XMLSocket class.
It's connect function in your script should look like
MyXMLSokcet.connect("Your PHP script URL",80);
befor you call this function you must place EventHandler for onData method.
after this method calls it will receive the number of jpeg files that
the php script have found and returned.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy