Archive

Archive for the ‘PHP’ Category

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 ,

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 , , ,

SEO Powered by Platinum SEO from Techblissonline