Archive for the ‘PHP Tutorials’ Category

May 10 Listing the Contents of a Directory With PHP Posted at 9:09 pm | No Comments »

Following is a quick tutorial explaining how to list the contents of a directory with PHP. Much of this information is taken from the official PHP documentation of the opendir() function.

To begin with, you need to determine which directory you want to list. Start by storing the location of that directory in a variable. If you want to list the contents of the directory in which your script is running, you can use the realpath function to find the full path to that directory. That would look something like:

$dir = realpath('./');

The function above will find the full, absolute path to the current directory and store it as the “$dir” variable.

Next, write yourself a function that looks similar to:

function listdir($dir) {
	$list = array();
	// Open a known directory, and proceed to read its contents
	if (is_dir($dir)) {
	    if ($dh = opendir($dir)) {
	        while (($file = readdir($dh)) !== false) {
	        	if($file !== '.' && $file !== '..' && $file !== 'index.php') {
	        		if(is_dir($file)) {
	        			$tmparr = listdir($dir.'/'.$file);
	        			if(count($tmparr)) {
		        			$list[] = '<li><ul>'.implode('',$tmparr).'</ul></li>'."\n";
		        		}
		        		unset($tmparr);
	        		}
	        		else {
		            $list[] = '<li><a href="./'.$file.'">'.$file.'</a></li>'."\n";
		         }
	         }
	        }
	        closedir($dh);
	    }
	}
	return $list;
}

The function above will recursively search the directory and create an unordered list of links to each of the files found inside. It will ignore “index.php” in each of those directories.

To run the function and print the list onto your screen, you would use code similar to:

$list = listdir($dir);
if(count($list)) { // Check to make sure the list returned had at least one item
    echo '<ul>'.implode('',$list).'</ul>';
}

That’s about all there is to it. I hope that helps someone.

Apr 28 Back Up A Large MySQL Database Posted at 7:53 pm | 2 Comments »

Anyone that’s dealt with a Web application using a MySQL backend has probably dealt with incomplete backups. Those of you that use phpBB have almost certainly encountered this issue. Unfortunately, within phpBB and quite a few other PHP/MySQL-based applications, the backup scripts fail miserably with any reasonably large amount of data. Most of them fail because of either PHP time outs or file size limits. This article will show you two different ways to successfully back up that data. At least one of these two methods should be available in almost every remote hosting package that uses MySQL and PHP. (more…)

Apr 22 Eight Links - April 22 Posted at 7:47 pm | No Comments »

Here are today’s Eight Links — links for web developers:

  1. JsPDF - create PDF files completely using JavaScript
  2. The Database Rant - begins with “It’s time for SQL to die.”
  3. iPhone app sales - a developer explains the income his app has generated and how changing pricing didn’t change sales
  4. Why writing software is like writing
  5. 18 Seriously Helpful Cheat Sheets for Easier Coding
  6. Google Analytics API Launched
  7. Finding and Fixing Memory Leaks in Python
  8. A Design for a Distributed Transaction Layer for Google App Engine

Mar 17 Check Your PHP Code For Vulnerability Posted at 6:30 pm | No Comments »

I’ve been meaning to review some of my older PHP code for security vulnerabilities for a while, but never really got around to it. This afternoon, I started searching for some tools I might be able to use to do that for me. I came across the Spike PHP Security Audit Tool, and was actually fairly impressed with it.

In order to run it, I believe you need to have the PHP command-line interface installed. However, as long as you’ve got that, all you need to do is upload the package and type a simple command. It will take a while, but when it’s done, the script generates a nice HTML report showing all of the vulnerabilities it detected. (more…)

Mar 5 Using MySQL with PHP - Part 1 Posted at 6:11 pm | 1 Comment »

This is a very simple tutorial intended to explain how to connect to a MySQL database with PHP and how to retrieve and manipulate information from that database.

Prior Steps

Before you actually connect to a database, you need to have a database to which to connect. Further, you need to have a table inside of that database that contains information. I will write up a simple tutorial explaining how to create new databases and add information to them in another tutorial, soon.

You will also need to know the username and password for a MySQL user that has, at least, SELECT permissions on the database and you will need to know the name of the “local” MySQL host (generally “localhost,” but it will sometimes deviate to things like “MYSQLHOST” or something like that). If worse comes to worse, you can try using the IP address of your Web server, if you know it.

Additionally, you need to have PHP installed and set up with the MySQL module set up (which, if you are not setting up your own Web server, is not generally something you ever have to worry about). (more…)

Feb 3 Some Handy Array Functions in PHP Posted at 6:28 pm | No Comments »

This post will go over the basics of some very nice array-related functions that are built in to PHP. If you use arrays fairly often in your PHP code, you will no doubt find a use for each of these handy functions over time. (more…)

Nov 16 PHP Basics for Designers Posted at 12:03 pm | No Comments »

The Atlanta Web Design Group recently held a discussion about PHP basics for web designers. Here are the slides from the presentation.

View SlideShare presentation or Upload your own.

Oct 22 Coding With CodeIgniter Posted at 6:04 pm | No Comments »

I’ve been coding away at my new startup CloudContacts for a good bit now and I’ve found that using the CodeIgniter PHP framework has been great. I’ve always been able to edit PHP code pretty well but starting an application from scratch has been a struggle. I watched a few CodeIgniter videos and was off. There are many libraries and downloadable code across the Web which helps even more as you build the app.

What’s great is that you can combine straight PHP code with CodeIgniter bits. I certainly am no expert but I think I have the basics down.

Except for Java, I can typically edit any code in most languages. Now I feel even more excited to be able to begin the application process. My belief has always been that every entrepreneur needs to understand, at least, enough code to discuss the application and more importantly decide which language to use for the application.

Hats off to my friend Jimmy who spent the good part of a day teaching me some of the more advanced features in CodeIgniter. I will provide more updates as I learn more with CodeIgniter.

Sep 24 phpCache — speed up your website Posted at 2:00 pm | No Comments »

Caching seems to be the new trend. But aside from all the marketing, it’s really a lot more than that. If you don’t have the money to buy those lovely Zend products, you might find yourself turning to the world of Open Source (again). And viola! You’ll be helped.

There are two ways to cache with PHP - as far as I know -, one is using PEAR’s Cache_lite class. There’s an article already about it on Devshed, but since someone already spoiled it and I am not too much of a pear-lover myself (I like wine-berries, oranges and stuff), I’ll talk about phpCache. So here we go.

So, at first we’ll start with the principals. What does this caching thing do? And why do I want it?
Caching is used in a million things. For example, there is your browser’s cache, which saves a website to your harddrive so next time, you go to this website, it’ll come up a lot more faster on your screen. Your ISP probably also offers caching through a proxy server. In this case, highly frequented websites are downloaded to the ISPs proxy server and delivered to you a lot faster than if you would go to the site directly. Thumbnails of larger images are a way of caching too.
(more…)

Sep 10 Object-Oriented Programming - Parents and Children Posted at 6:43 pm | No Comments »

This post is going to be a somewhat short abstract about the concept of parents and children in object-oriented programming (OOP). The first thing of which people new to OOP need to be aware is that parents and children are are not handled as logical parents and children, they are handled more like biological parents and children. It is best to think of the parent/child relationship in OOP as a concept of “inheritance” rather than a concept of containers and contained elements.

(more…)