PHP – FTP Class Example

One of the more useful extensions for PHP is the FTP modules. There are many uses for this, mainly uploading and downloading files to and from a remote server – Although could be used to backup websites, restore backups, file management, the list is endless.

I’ve prepared a basic class below that utilises the FTP functions, developing a class in this way allows us to create a simple and manageable FTP session cutting down on all un-needed and repetitive code.

Read More

PHP – Example Practical use for Similar Text

One of the more neat features of PHP is these functions; the ones that can analyse strings for you, taking away all of the hard work. While they’re neat to use, they may not always have a practical use in a project.

I was thinking of ways I could use similar_text() in a project, one of the examples I came up with is making a ‘Did you mean?’ part to navigation. The idea is, that when somebody types a wrong URL slug it’ll check the page names to see if any are close enough to match, if they are It’ll suggest that page.
Read More

PHP – Checking POST and GET Fields

This is another very basic piece of code that will probably come in handy when manually checking form fields for data and used to determine if there is actually any data in the field.

Normally you would have a block of code, normally an If or Switch statement that will check to make sure the variable isn’t empty() and that it is isset() – This function will allow you to do that in batch format, just pass in an array of required fields to check.

Read More

PHP – Detect Ajax Request (Shorthand)

It’s always useful to detect whether or not your script is directly accessed or accessed using Ajax. This function will detect the SERVER variables for HTTP_X_REQUESTED_WITH and use PHP shorthand syntax to return a boolean true or false value.

This may come in handy for outputting different contents depending on how it was requested, or in-fact another hundred plus uses. Code below.
Read More

PHP – Better Array Printing

There are always times when you need to print out an array to screen to either check it’s contents or simply get the right keys when working with your code. The most useful functions to do this are var_dump() and print_r() which will both print out arrays.

The problem is, to be able to read the var_dump() properly you’ll have to most likely view the source of the web page, with print_r you’ll have to wrap the contents with pre to make it readable in a structured format. I wrote a very simple function to take multiple array arguments and print them out cleanly and in an easy readable format.
Read More

Recursive Folder Scan using Recursive Directory Iterator

There are a few ways to list folder contents using PHP. One of the most simplest (and more under used) is the Recursive Directory Iterator provided by the SPL PHP library. I have included a sample function below of how to scan a folder (recursively) and return an array of full paths and file names.

The function below will allow you to specify if you want to return full paths, or relative paths – There is also an option to return only file names.
Read More