Category Archives: PHP

How to test which HTML form button was pressed in PHP

execImagine you have a HTML form with several values and two buttons at the bottom. One could say “Do This” and the other “Do That”. How do you know which one was pressed by the user?

It’s fairly easy – but I keep forgetting this time and time again. We do this by accessing the name attribute of the HTML button in PHP.

Consider this HTML form:

The button classes are WordPress standard “blue” and “grey” button layouts, and the value is what’s written on the button. The secret sauce however is in the name fields here.

Back in PHP when the form is submitted we can access the $_POST variable which an array of values we’re getting back from the form. We can access them like this:

You can access the rest of your form’s elements in the same way (i.e. tick boxes, select options, text fields, etc).

How to create a recursive ZIP Archive from a directory in PHP

PHP-IconI’ve just realised how (relatively) easy it is to make PHP create a ZIP Archive from a directory and its sub directories.

Previously I had relied on the Linux Shell Command zip or tar to do this which is a convenient one liner. I had assumed that in PHP we had to go through each directory and add the files manually – until I’ve discovered the RecursiveIterator class variants.

In this example I’m defining a root path and a file name for my archive (change at will), then iterate through each file in each directory (and if thats’ a directory too, we’ll go one level deeper). The result of which is an array of files. We’ll get their full paths and add them to the ZIP archive.

The above will produce a ZIP file which will contain references to the absolute full paths of each file. That’s great if you’d like to unzip those files later in exactly the same location – but it’s not so cool if you’re just interested in the content and the idea of 11 sub folders strikes you as “less funny”.

Help is at hand, thanks to the way we can add files to the archive – namely by creating a new local filename. Here’s an example of how this works on Linux systems:

No more references to absolute full paths in your ZIP file.

  • http://www.php.net/manual/en/class.ziparchive.php
  • http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php
  • http://stackoverflow.com/questions/3993105/php-creating-zips-without-path-to-files-inside-the-zip
  • http://us3.php.net/manual/en/class.recursivedirectoryiterator.php

How to add elements to an array in PHP

PHP-IconIt’s extremely easy to add elements to an existing array in PHP – so easy in fact that I regularly forget how to do it!

I’m used to initialising and setting up my variables from Objective C, but PHP is so much easier and deals with it on the fly. Here we do it – ideal for loops:

  • http://stackoverflow.com/questions/676677/how-to-add-elements-to-an-empty-array-in-php

Getting Started with ZEND Server 6 on Mac OS X

ZEND_logo

I’ve just installed ZEND Server 6.3 on my MacBook running Mavericks 10.9.1. Needless to say I’m sill a little shaken up from the huge amount of brain pain this adventure has caused.

Because once downloaded and installed on your system, you may ask yourself a vital question: Now What?

Let’s find out. This article is Work in Progress – bear with me while I flesh it out.

The Basics

ZEND Server on Mac is located here:

  • /usr/local/zend/

Your web files live here:

  • /usr/local/zend/apache2/htdocs

To open this directory in Finder you can navigate there with a Terminal session and open it, like so:

Now you can create a shortcut on your sidebar for easy access.

To access the ZEND Server Admin interface, navigate to the following URL in your browser:

  • http://localhost:10081/ZendServer

If you’re done with it, you can uninstall ZEND Server with the following command:

Continue reading Getting Started with ZEND Server 6 on Mac OS X

How to install ZEND Framework for use with MAMP

ZEND_logoIf you have MAMP installed and working on your Mac, it’s easy to get started with ZEND Framework development. I’ll show you how in this article.

Download the framework from here:

  • http://framework.zend.com/downloads/latest

Choose the full version without ZEND Server (not necessary as we’re using MAMP). Unpack the download and put it somewhere safe. I’m adding mine to my Documents directory. I’ll also rename my folder to something like “ZendFramework” without the version number.

To access it from anywhere on our machine we’ll create an alias named “zf”. zf is a shell script provided by the framework that we’ll need throughout our development journey with ZEND. Let’s to this in a Terminal session:

Replace the path with your own. Notice the call to /bin/zf.sh which is the “real” shell script. Our alias has just made this universal and accessible without having to mess with our shell path.

Verify that it’s working with this:

Read the full article

How to send an email in PHP

Many complex things are extremely simple in PHP – sending mail is one of them. Here’s how:

The mail function will add the message to the out queue, so the test will not show if the message has actually been sent.

To avoid really long single line emails (i.e. entire message on one line) we can use the wordwrap() function, causing an automatic wrap to the next line if more than 70 characters are in a single row:

All tips courtesy of the PHP Manual Pages:

  • http://php.net/manual/en/function.mail.php

How to test if a Shell Command can be executed in PHP

Before we execute a shell command from PHP it’s a good idea to test if the server will respond to it. We can do this by making use of the empty() function.

The following example consists of a helper function you can call before executing the command in question. Then we call it with the shell command we intend to use, before executing the command for real. We’re using ‘uname -a’ here as an example that will generate output and takes a parameter:

  • http://us1.php.net/empty
  • http://stackoverflow.com/questions/12424787/how-to-check-if-a-shell-command-exists-from-php

How to test if your server is running Windows from PHP

If we’re executing shell commands via PHP we need to know if the server is running Windows or Linux. Thanks to a magic constant in PHP we can find out like this:

This will give us a single value like

  • Linux
  • Darwin
  • Windows
  • WINNT

With that info at hand, we can write a function that delivers a boolean result (courtesy of the PHP manual):

This takes the output of PHP_OS and looks at the first three characters turned into upper case. If those are identical to WIN it’s Windows – and if not we assume it’s a Linux flavour.

If you need a more detailed information about your environment consider using php_uname():

This will give you the above, plus server name, kernel version, release and local server time.

  • http://www.php.net/manual/en/function.php-uname.php
  • http://stackoverflow.com/questions/738823/possible-values-for-php-os

How to read the contents of a file into a string in PHP

file_get_contents() can help us here. It reads a file and stores it in a string. Can be used with local files, as well as online content:

This is different from the file() function which reads the contents of a file into an array.

  • http://us2.php.net/manual/en/function.file-get-contents.php
  • http://us2.php.net/manual/en/function.file.php

How to replace text inside a string in PHP

If we have a string and would like to replace a portion of it, we can use the str_replace() function in PHP. It works like this:

str_replace (‘whatToReplace’, ‘theReplacement’, ‘originalText’);

It’s easy to remember… but I always get confused when I look this up in the manual. Here’s an example:

This works well if there’s only one occurrence that can be replaced. If you had ‘xxxx’ twice in the original text, both xxxx’s would be replaced by Something:

This function is cAsE SenSitiVe – you can also use str_ireplace() which will ignore case.

  • http://php.net/manual/en/function.str-replace.php
  • http://www.php.net/manual/en/function.str-ireplace.php

You can also do it with Regular Expressions (shudders):

  • http://stackoverflow.com/questions/3426265/php-string-replace-match-whole-word