PHP

Apache PHP mkdir not writable?

If you ever find that you are creating a folder with PHP, and its immediately not writeable by Apache, then you’ve probably done what I did with the chmod. It needs to be an octal value, and PHP will not assume it is. So you have to prefix with a zero.

mkdir("/path/to/folder/structure/", 777, true);

Should actually be:

mkdir("/path/to/folder/structure/", 0777, true);

Took me a while to work it out, I thought it was something to do with Mac permissions…

Add a path to the PHP include path

I recently wanted to add a Zend library to a project without adding the entire framework.

I found that internally, the Zend application requires the Library path to be in the include, as it uses relative includes. So, easy to fix without editing the actual pages. I just needed to add the folder they were in, to the PHP include path.

function add_path_to_include ($path)
{
    //get the existing paths using the PATH_SEPERATOR constant
    $paths = explode(PATH_SEPARATOR, get_include_path());

    //if the new path isn't already there
    if (array_search($path, $paths) === false) {

        //add it on
        array_push($paths, $path);
    }

    //add them all back on
    set_include_path(implode(PATH_SEPARATOR, $paths));
    }

Jobs a goodun…

Compile php_imap and mssql for PHP on Mac

IMAP

To get imap support in PHP on your Mac, you need to get the php_imap module compiled for your version of PHP. I have attached mine to this to save you the effort, but this wiki article can remain for reference.

Firstly, you need to download the Client-c code. This is a library that allows various applications to communicate with IMAP servers. PHP being one of them.

ftp://ftp.cac.washington.edu/imap/imap-2007e.tar.gz

Save it into your downloads folder, and once finished, extract it. If you are using Terminal, you can do the above with the following commands: (NB, do all this in sudo)

cd ~/Downloads
curl -o imap.tar.gz ftp://ftp.cac.washington.edu/imap/imap-2007e.tar.gz
tar -zxvf imap.tar.gz

Then we need to compile it, and copy all the C files to the right places.

cd imap-2007e/
make osx EXTRACFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
mkdir /usr/local/include
cp c-client/*.h /usr/local/include/
mkdir /usr/local/lib
cp c-client/*.c /usr/local/lib/
cp c-client/c-client.a /usr/local/lib/libc-client.a

Now we need the version of PHP we need. To find out what version you are running, run this in a Terminal window:

php -v

That’ll tell you which version you need. Replace the curl address below with the relavant version from the php.net website.

cd ~/Downloads
curl -o php.tar.gz http://uk.php.net/distributions/php-x-x-x.tar.gz
tar -zxvf php.tar.gz

Once that is all extracted, we need to compile PHP:

cd php-x.x.x/
sudo ./configure
sudo make

Once that’s done we can go to the imap extension folder and compile that extension. There are more extensions in the file structure, but for now, we just need the imap one:

cd php-x.x.x/ext/imap
phpize
CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
LDFLAGS="-arch i386 -arch x86_64 -bind_at_load"
./configure --with-imap=/usr/local/imap-2007 --with-kerberos --with-imap-ssl=/usr/
MACOSX_DEPLOYMENT_TARGET=10.6

That should create a nice little imap.so file in the modules folder:

/Users/chris/Downloads/php-x.x.x/ext/imap/modules/imap.so

This file needs copying to the extensions folder on your PC. So you need to find the right folder. Mine was:

/usr/lib/php/extensions/no-debug-non-zts-20090626/

So, I copied the file there:

cp imap.so /usr/lib/php/extensions/no-debug-non-zts-20090626/

And then all we need to do is edit the php.ini file, to load the extension:

nano /etc/php.ini

Scroll down to the extensions bit, and add it in, mine would be like:

extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/imap.so

Then reboot Apache

apachectl restart

That should be it.

Next, php_mssql (if required)

MSSQL

The process is really very similar to what we just did for imap, but we do need to install freetds which is a mssql thing

Create the directory /usr/local/freetds
Download the latest version of freetds (http://www.freetds.org/software.html)
Open a terminal window
Untar/zip the freetds file and cd into that directory in terminal

Run the following to compile the extension

 ./configure --prefix=/usr/local/freetds --sysconfdir=/usr/local/freetds/conf/freetds --disable-libiconv --disable-odbc
make
sudo make install

Find the PHP source code from earlier, and go to the correct folder:

cd /Users/chris/Downloads/php-x.x.x./ext/mssql/
sudo phpize
sudo ./configure --with-mssql=/usr/local/freetds
sudo make

Now its the same as the imap section. Find the mssql.so file in the ext/mssql/modules/mssql.so and copy it to the extensions folder.

cp mssql.so /usr/lib/php/extensions/no-debug-non-zts-20090626/

And then all we need to do is edit the php.ini file, to load the extension:

nano /etc/php.ini

Scroll down to the extensions bit, and add it in, mine would be like:

extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/mssql.so

Then reboot Apache

apachectl restart

And that should work now. Create a page with a simple echo phpinfo() and that should confirm the fact.

Errors trying to access Google IMAP through PHP

imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX

This error /can/ be missleading. I thought something was up with the stream (guessing there was a misconfiguration with PHP, IMAP and openSSL) – but it turned out I had typed the wrong password in the initial request.

Just incase anyone else is struggling here, just check that first.

Then, ensure you have installed the openSSL and the php5-imap extension correctly.

AllowEncodedSlashes not taking effect?

If you are switching on the AllowEncodedSlashes setting in the apache conf file, and it doesn’t seem to be making any difference, then it may well be because you are using vhosts, and the setting is not automatically propergated through them.

You’ll need to edit the vhost and specify the setting within the config part of the vhost:

<VirtualHost *:80>
    ServerName mytest.local
    DocumentRoot /var/www/site2/
    AllowEncodedSlashes On
</directory>

Be sure to read all about it, and understand the security risks.

PHP Setting Error Reporting at runtime

You can use the PHP function error_reporting() at run time to set the level of error reporting in your application.

The different levels of reporting are based on bit masks or using a CONSTANT name. They are:

1       E_ERROR
2       E_WARNING
4       E_PARSE
8       E_NOTICE
16      E_CORE_ERROR
32      E_CORE_WARNING
64      E_COMPILE_ERROR
128     E_COMPILE_WARNING
256     E_USER_ERROR
512     E_USER_WARNING
1024    E_USER_NOTICE
2048    E_STRICT
4096    E_RECOVERABLE_ERROR
8192    E_DEPRECATED
16384   E_USER_DEPRECATED
32767   E_ALL

If you want to hide all errors : use zero:

error_reporting(0);

Otherwise, you can just use the bitmask or the name:

//show RECOVERABLE
error_reporting(4096);

//SHOW RECOVERABLE AND USER ERROR
error_reporting(4096 + 256);

//show warnings and deprecated
error_reporting(E_WARNING | E_DEPRECATED);

//show all except Notices
error_reporting(E_ALL ^ E_NOTICE);

Convert HTML table to Excel XLS

I recently needed to update a page that dumped a HTML table into Excel – and found it dumped everything, including hyperlinks, buttons, images, etc. This didn’t sit well with Excel for Mac, and probably not a good idea.

I thought I would re-write it, but in a way that was re-usable (as this code was situated on every datagrid in the application)

So, using the PEAR extension Spreadsheet_Excel_Writer and a simple HTML DOM class, I came up with this. Its by no means finished, but as it stands, it works pretty well for what I need. Feel free to rob it and use the code.

The call is from a form which uses jQuery to copy the table as HTML into an input and then uses that in the output script ($_REQUEST[‘gridcontent’])

When its all complete, I may put it on an open source repo, such as sourceforge.

FYI, there is a section in here that takes cell text with a
in it as a split cell. So if your TD has a BR in it, it will make all the other rows merged so that the data looks nice. This is by design.
Continue reading