Delete all files matching patter using PHP glob()

When you have the need to do a little house cleaning on your storage system programmatically you can use PHP’s glob() and array_map() functions to make easy work of this.

In this example, we are going to find all text files in “some/dir/” and unlink (aka: delete) them. The PHP glob() function gets us our list of files and then the array_map() function passes each index value to the callback function, in this case, the “unlink” system command.

Syntax: 

array_map(callback, array)

glob(pattern,flags)

Example:

The result of this

Get a list of files or directories using PHP glob()

The PHP glob() function returns an array of filenames or directories matching a specified pattern.

This function returns an array of files or directories or FALSE on failure.

Syntax: 

glob(pattern,flags)

Example:

The output of the code above could be:

 

Checking for Oversized Uploads using PHP

It’s always good practice to validate the uploads that you allow from the end-user are not exceeding your application or system limitations.

Here is an example of how to check if an uploaded file exceeds the default limit value held in your php.ini configuration.

 

Force UTF-8 onto a MySQL Table

I have come across an instance where converting to UTF8 during the MySQL import process does not always “convert to UTF8” even though the table charset is set to UTF8.  The following query will fix a column with data stored latin1 in a utf8 table:

 

 

Presentation is Powerful

We can not present that code formatting is unimportant, but must understand why it matters.  A good code format is not the one you think looks the prettiest.  We do not do not layout code in order to exercise our deep artistic leanings.  Good code is clear.  It is consistent.  The layout is almost invisible.  Good presentation does not draw attention or distract; it serves only to reveal the code’s intent.  This helps programmers work with the code effectively.  It reduces the effort required to maintain the code.