List files in a directory using php

September 13th, 2009

To display a list of files within a directory using PHP you need to use opendir() and readdir(). Below is a function that will return the list of files and remove the . and .. from the listing.

 
//full path of directory
$dir = "/var/www/images/";
 
function listFiles($dir){
 
//open directory and read its contents
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
 
//only display filenames
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
}
 

You might run into trouble trying to find the correct full path of the images. For example in a windows hosting environment the path could be  (c://blah/blah), in a Linux environment it could be (/var/www/blah/blah).

To find the exact path for your environment use phpinfo(). Once you have the php info look for the DOCUMENT_ROOT entry.

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz PHP , , , ,

Dynamic variable names in PHP

August 18th, 2009

I'm going to show you how to use dynamic variable names in php. It's quite simple, just put your variable name in between {} symbols.

// declare prefix. In a loop this would probably be a number.
$prefix = "pfx";
 
// build your variable
${"varname_{$prefix}"}    = "success";
 
//This would output "success"
echo $varname_pfx;

Simple isn't it....

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz PHP ,

Scaling a dynamic background image in proportion using flash

August 13th, 2009

Recently I was playing around with scaling background images and had issues with how the scaling behaved and image quality.  When scaling an image in proportion you want to make sure that the image container is centered to the movie clip it belongs to.

My example below is the scaling code within a stage listener which will re size the background image according to the stage width & height.

Stage.align = "TL";
Stage.scaleMode = "noScale";
 
//Get Background Image Container Movie Clip Name (instance)
var backgroundImage:movieClip = "MovieClip Instance Name"
 
// Create an object for the stage listener
stageListener:Object = new Object();
 
// Create a function for the event you want to listen for
stageListener.onResize = function() {
 
//Get the  stage width & height
var sw:Number = Stage.width;
var sh:Number = Stage.height;
 
//Check to see if image width or height needs adjusting
if ((sh / sw) > (backgroundImage._height / backgroundImage._width)) {
 
//get image width and adjust height to fit
scale = backgroundImage._width / backgroundImage._height;
backgroundImage._height = sh;
backgroundImage._width = sh * scale;
} else {
 
//get image height and adjust width to fit
scale =backgroundImage._height / backgroundImage._width;
backgroundImage_width = sw;
backgroundImage._height = sw * scale;
}
 
//Center the background Image
backgroundImage._x = (sw - backgroundImage._width) / 2;
backgroundImage._y = (sh - backgroundImage._height) / 2;
 
};
// Add listener for the Stage object
Stage.addListener(stageListener);

Now the the simple line of code that makes this work so well is the quality preservation part. Just add this after your dynamic image has loaded.

backgroundImage.forceSmoothing = true;

An actionscript 3 version would not be much different to this and should be fairly easy to workout. If your struggling just let me know.

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz ActionScript 2.0 , , ,

Images not working in dompdf

August 6th, 2009

What is dompdf?  Well basically its a HTML to PDF converter. It's rendering engine is built in PHP and is style-driven which means it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.

I won't go into detail of how to use it (unless you want me to or would like to write about it), but recently I  had an issue with images not rendering  in the pdf. At first I thought it was a permissions problem, then I thought it was an image type problem. It turns out it was a file location issue.

instead of the file location being:

<img src="images/myimage.jpg" style="width:200px;height:200px">

It should be:

<img src="/var/www/images/myimage.jpg" style="width:200px;height:200px">

So as you can see it needs to reference the file from the server side file directory and not just from within the web directory.

Wish someone wrote this, when I needed it....

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz PHP , , ,

Using swfObject with Mootools

August 4th, 2009

I'm sure most of us use swfObject, which in my opinion is the best option when it comes to embedding flash.

Lately I've been having a lot of fun with mootools and needed a way to embed flash dynamically. Basically I wanted my flash file to be embedded after a mootools click event was triggered.

So without further ado..

window.addEvent('domready', function() {
 
//get the div I want to embed my flash into.
var flashcontainer = $('flashcontainer');
 
//set all the flash properties
var file = 'file.swf';
var moviename = 'nameMyFlash';
var width = '640';
var height = '424';
var version = '9';
var bgcolor = '#000000';
 
//create the function that will fuse swfObject with mootools
var embedFlash = function(){
    var so = new SWFObject(file, moviename, width, height, version, bgcolor);
	     so.addParam("quality", "high");
	     so.addParam("align", "middle");
	     so.addParam("play", "true");
	     so.addParam("loop", "true");
	     so.addParam("scale", "exactfit");
             so.addParam("allowFullScreen", "true");
	     so.addVariable("variable1", "variable 1 value goes here");
	     so.addVariable("variable2", "variable 1 value goes here");
	     so.write(flashcontainer);
}
 
//get button by id
var myButton = $('mybutton'); 
 
// create click event to fire my function called "embedFlash()"
myButton.addEvent('click', embedFlash); 
 
});

And that's it.. Hope it helps....

You can download the latest tools used above from the links below:
MooTools - download release 1.2.3 or SWFObject 2.2

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz ActionScript 2.0, ActionScript 3.0, Mootools , , ,

FLA crashes when publishing, saving or simply viewing the action tab

April 7th, 2009

Today I came across an annoying issue with a CS3 file that I had edited a month ago. I was required to make simple edits so I re-opened the fla file. It crashed every time I tried publishing, saving or simply viewing the action tab and could not figure out why. After hours of debugging I finally fixed the problem by simply copying it from the network to my desktop. Basically every time I tried opening the fla file from the network it crashed. I don't have an exact reason why this is happening because other files work fine, however this is the solution to fix it.

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz News ,

Building a Preloader in ActionScript 3

March 1st, 2009

We are going to explore how to build a preloader in as3.  Before building the preloader you will need to understand how to load external images/data with ActionScript 3.

If you read the loading external images post you will know that the image was loaded into the contentLoaderInfo property of the Loader object. This is what the code looks like.

var imgLoader:Loader =  new Loader();
imgLoader.contentLoaderInfo.addEventListner(Event.COMPLETE,
loaderCompleteHandler);
 
function loaderCompleteHandler(e:Event):void{
trace('Image has loaded.');
}
 
imgLoader.load(new URLRequest('image.jpg'));

To monitor the loading progress of this externally loaded image we need to use the LoaderInfo object which is a package of information about the load that is passed to the event listener of contentLoaderInfo. The properties of the LoaderInfo class along with the event object will provide all the information needed to build our preloader.

The available properties of the LoaderInfo class are:

  • bytesLoaded
  • bytesTotal
  • content
  • frameRate
  • height
  • width
  • loader

The available event types of the LoaderInfo class are:

  • COMPLETE
  • INIT
  • PROGRESS
  • UNLOAD

From the event types above we need to use the PROGRESS event type like the following.

imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
preloaderHandler);

Then to display the bytes being loaded we need to use the bytesLoaded and bytesTotal properties, and trace them when the function preloaderHandler is triggered. If you divide the 2 and multiply by 100 you will get the percentage loaded.

function preloaderhandler(e:ProgressEvent):void{
trace('bytesLoaded:  ' + e.bytesLoaded);
trace('bytesTotal:  ' + e.bytesTotal);
trace('percentage loaded:  ' +
Math.round((e.bytesLoaded/e.bytesTotal)*100) + "%");
}

The complete source code looks like this.

var imgLoader:Loader =  new Loader();
imgLoader.contentLoaderInfo.addEventListner(Event.COMPLETE,
loaderCompleteHandler);
imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
preloaderHandler);
 
function loaderCompleteHandler(e:Event):void{
trace('Image has loaded.');
}
 
function preloaderhandler(e:ProgressEvent):void{
trace('bytesLoaded:  ' + e.bytesLoaded);
trace('bytesTotal:  ' + e.bytesTotal);
trace('percentage loaded:  ' +
Math.round((e.bytesLoaded/e.bytesTotal)*100) + "%");
}
 
imgLoader.load(new URLRequest('image.jpg'));

Enjoy. I'll write a post soon on how to integrate this preloader script with a simple animation.

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz ActionScript 3.0 , ,

Building a simple clock application in as3

February 25th, 2009

Yesterday I posted how the Date class worked in ActionScript 3.  Today I though I would build a simple clock application using the Date class.

Building the clock app involves using the Date class in a couple of ways. The Date class returns the current date and time of when the method is called and only once per object created. Basically the example I gave in my previous Date class post performed this once.

To have our clock continually roll through the time and date, a new Date object must be created and the methods of the Date class that return the date and time properties must be called at regular intervals to display the changing time. To create our clock application, we will use the Timer class.

The following code will create a TextField object to display the time and a Timer object that updates every 1/10th of a second in the updateTime() event handler for the Timer event of the Timer class.

When the updateTime() event handler is called on each TIMER event, a new Date object and a new variable for the time are created, concatenated, and set as the text property for myDisplay.

A new Date object needs to be created each time because the Date class stores only the time and date values for the time in which the object was created.

Alright.. Let's get into the code..

var myDisplay:TextField = new TextField();
var myTimer:Timer = new Timer(100);
 
myTimer.addEventListener(TimerEvent.TIMER, updateTime);
 
function updateTime(evt:TimerEvent):void{
 
var localDate:Date = new Date();
var hour:int = localDate.getHours();
var minute:String;
var second:String;
 
/* Note that if the second and minute
values are less than 10 we add a 0 to the
single digit */
 
if(localDate.getMinutes()&lt;10){
	minute = String(0)+localDate.getMinutes();
}else{
	minute = String(localDate.getMinutes());
}
 
if(localDate.getSeconds()&lt;10){
	second = String(0)+localDate.getSeconds();
}else{
	second = String(localDate.getSeconds());
}
 
myDisplay.text = hour+':'+minute+':'+second;
 
}
 
myTimer.start();
addChild(myDisplay);

Hope this helps ...  Peace...

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz ActionScript 3.0 , , , ,

The Date Class in AS3

February 24th, 2009

The Date class will allow you to check the current date and time on a user's computer. You'll find this class valuable when developing widgets like an event count down clock. To work with the Date class, you must instantiate a Date object:

var myDate:Date = new Date();
trace(myDate);

When the Date object has been built, you can call methods or get properties of the Date class to get date and time values:

var myDate:Date = new Date();
trace(myDate.getDate());

The getDate() method gets the current day of the month.

The getMonth() method gets the month of the year as a number value. Numeric references to the current month are not the normal month numbers we are used to working with.

For example, January is equal to 0 and December is equal to 11. For display reasons, always add 1 to the returned month number to get the true calendar month value.

Available methods for getting Time and Date properties of the Date class are:

  • getDate();
  • getDay();
  • getFullYear();
  • getHours();
  • getMilliseconds();
  • getMinutes();
  • getMonth();
  • getSeconds();

Note: For a full description of each method search "date class" via the Flash help menu.

Easy.. now get fancy with it and tell us what you created..

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz ActionScript 3.0 , , ,

tar: Archive contains obsolescent base-64 headers

February 24th, 2009

I was trying to install Ghostscript on my MediaTemple (mt) Linux server but I kept getting the following error:

tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: Error exit delayed from previous errors

The quick fix around this is to type the following:

gzip -d filename.tar.gz

and then

tar -xf filename.tar

This should extract the file without errors.

Peace

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post

Flashnutz Linux

SEO Powered by Platinum SEO from Techblissonline