Author Archive

PHP behaves erratically when writing to files

This is such a dumb error  but I wanted to write it in case anybody else becomes mystified if a stable PHP application starts suddenly behaving erratically when writing to files.

The cause of this weird behavior could be that your server’s hard drive has run out of free space.   To check your server’s drive space on a Unix machine, you use the “df” command.  This will show you a percentage of used space.  If it’s in the high 90’s then obviously you need to clean out some files or upgrade the hardware.

This can cause a wide variety of very strange symptoms including:

  • Permission denied errors are thrown when opening, writing or closing files even though PHP has full permission granted
  • PHP stops appending to log files
  • PHP can create new files, but they are empty even though you are expecting PHP to write some data)
  • PHP can’t start new sessions, or doesn’t remember user’s session (because it can’t write session files)
  • Unknown error in Line 0.  (This actually occurs if you have an exception in your destructor, however this is a common place to close file handles so you might see this error)

Obviously the server is in an unreliable state so it’s likely to cause all kinds of weird and seemingly random behavior.  If you notice any other symptoms feel free to post a comment.

Install PDT Plugin in FlexBuilder 3

PDT Screenshot

Installing PDT (PHP Development Tools) Plugin for FlexBuilder allows you to work with PHP projects within the FlexBuilder environment.  This is useful if you are using AMFPHP, WebORB for PHP or some other PHP-based remote server.  There are similar instructions posted elsewhere, however I found that they have become outdated.  Because FlexBuilder 3 is based on an older release of Eclipse, you now have to specifically install older versions of the PDT files.

Note – If you are using a recent version of Eclipse with FlexBuilder plugin, or are using FlexBuilder 4 then you should not follow these instructions – instead install the most recent version of PDT.  These instructions are only necessary for FlexBuilder 3 Stand-Alone edition.

Installing PDT:

  1. Open FlexBuilder 3 menu Help->Software Updates->Find and Install
  2. Search for new feature to install
  3. Click “New Remote Site” and enter the following:
    • Name: PDT 1.x
    • URL: http://download.eclipse.org/tools/pdt/updates/
      (note is that this URL is to the old 1.x update site)
  4. After adding the site, click OK and select the following two Sites to include:
    • Europa Discovery Site
    • PDT 1.x
  5. Click “Finish” to begin searching for updates
  6. Once the results are displayed, un-check the box that says “Show latest version of a feature only”
  7. Expand PDT 1.x and select PDT SDK 1.0.3
    (important – do not select a higher version than 1.0.3)
  8. After you have selected PDT SDK, you may have required libraries missing.  You can try at this point clicking “Select Required” and they will be automatically selected from the Europa Discovery Site.  However, this sometimes causes FlexBuilder to lock up.  In which case, you have to force quit FlexBuilder, start the process over and manually select the required libraries which are:
    • Graphical Editors and Frameworks->Graphical Editing Framework 3.3.2
    • Java Development->Eclipse Java Development Tools 3.3.2
    • Models and Model Development->Eclipse Modeling Framework (EMF) Runtime + End-User Tools 2.3.2
    • Models and Model Development->XML Schema Infoset Model (XSD) Extender SDK 2.3.2
    • Web and JEE Development->Web Standard Tools (WST) Project 2.0.2
      (if any of these libraries are missing and do not appear, it probably means that you have already installed them)
  9. You should now be able to click “Finish” and agree to the license.  You can then restart the app and begin working with PHP project within FlexBuilder.

Configure Smarty (TPL) Template Editing

There is another project called smartypdt – however it doesn’t seem to be compatible with the older version of PDT – at least I couldn’t get it working.  Instead I simply configure TPL files to open using the PHP editor:

  1. Open the menu Window->Preferences
  2. Navigate in preferences to General->Content Types
  3. Expand the Text node and highlight “PHP Content Types”
  4. In the File associations list, add *.tpl

At this point you should be able to open a PHP folder and edit both PHP and TPL files.  If you configure your include path correctly, you should have code insight on your PHP classes and functions.

sqlite lpad and rpad functionality

sqlite is missing several basic database features compared to typical SQL servers and unfortunately padding is one of them.  For a most situations though, you can use a simple hack to get the same functionality.   This trick uses a combination of concatenation and substr:

-- the statement below is almost the same as
-- select lpad(mycolumn,'0',10) from mytable
 
SELECT substr('0000000000' || mycolumn, -10, 10) FROM mytable
 
-- the statement below is almost the same as
-- select rpad(mycolumn,'0',10) from mytable
 
SELECT substr(mycolumn || '0000000000', 1, 10) FROM mytable

The statement is fairly self-explanatory, but in case it doesn’t make sense we’re simply adding a big long string of characters to the original value and then truncating it down to the desired length.  The string used for concatenation has to be at least the same length that you are padding – 10 characters is used in this example (‘0000000000′).

In most cases this workaround produces the same results as lpad/rpad except in the case where the length of your original value is greater than the length that you are padding.  In which case the original value would get truncated.  Usually when you are padding you know what the maximum length of the column anyway.

If you know of a more efficient technique, please post a comment.

Installing Flex Formatter Plugin

If you like your code clean and with proper doc block comments, Flex Formatter is a handy plugin for Flex Builder 3.  The plugin installs in Eclipse by simply dragging the files into the plugin directory, however with Flex Builder you can install from a remote install site hosted on the google code site.

To install in Flex Builder 3:

  1. Help -> Software Updates -> Find and Install…
  2. Choose “Search for new features to install” and click Next
  3. Click “New Remote Site” button and enter the following in the dialog:
    • Name = Flex Pretty Print Command:
    • URL = http://flexformatter.googlecode.com/svn/trunk/FlexFormatter/FlexPrettyPrintCommandUpdateSite/
  4. Click Finish and complete the process of installing the plugin

After installing and restarting Flex Builder you’ll have five new icons in your toolbar.  These allow you to add doc block and clean up spacing for selected sections or an entire file.

The settings for Flex Formatter are found along with all of the other Eclipse preferences.  You can choose to have files formatted automatically on save, specify locations to exclude and a few other options.

The Auto Format settings allows you to configure the template used when cleaning your code.  This allows you to specify exactly how you like your files formatted.  This is an extremely useful feature for organizations that enforce a certain code style.

Flex: All your dates are belong to us

I feel like I’m constantly ranting about Flex’s implementation of Dates.  This week I just discovered a new “feature” that is even more annoying to me.

Without going over it all again, Flex dates are timezone aware and the UTC offset is automatically calculated for you.  The problem is that you are locked into displaying the timezone of the local system clock unless you hack the date values.  But when sending over the wire to the server the date is automatically converted into the server’s timezone without telling you what timezone the Date is supposed to be in. In other words, you get “8:00 PM UTC” but you have no ideas what timezone that pertains to, so you don’t know whether the Date is actually supposed to read 2:00 PM EST, 1:00 PM CST, or 11:00 AM PST. Technically those are all the same “time,” but in some cases you need to work in timezones other than your own.

Anyway, I implemented my own solution for this issue last year, but was going on the assumption that the timezone offset is based only on the local system clock.  What I found out last week is that the timezone offset will actually change if you create a Date in the past or future where DST has kicked in/out.  In other words, every Date has it’s own offset.  I’ll admit that could probably be handy however if you are trying to neutralize the timezone changes that Flex does, it only makes your job that much harder.  I had not been expecting this and so we had to do some modification to our client to correctly handle DST changes at the server.

Below is some code to demonstrate how the timezone offset changes with DST, even though your system clock is still the same.  Note that you need to set your system clock to a timezone in the US that observes daylight savings time on March 15.  (Chicago for example). If you’re in the UK the swtich is March 28, so you can change the dates to March 27 & March 28 to see the effect. Notice that the timeZoneOffset property is different between the two dates. I suppose this makes sense but it definitely creates hassles on the server side.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
      layout="vertical"
      height="700"
      width="1000"
      creationComplete="onCreationComplete()">
 
 <mx:Script>
  <![CDATA[
   public function onCreationComplete():void
   {
    var d1:Date=new Date(2010, 2, 14); // march 14
    result1.text="MARCH 14: " + d1.toDateString() + " OFFSET = " + d1.timezoneOffset.toString();
 
    var d2:Date=new Date(2010, 2, 15); // march 15
    result2.text="MARCH 15: " + d2.toDateString() + " OFFSET = " + d2.timezoneOffset.toString();
   }
  ]]>
 </mx:Script>
 
 <mx:Label id="result1"/>
 <mx:Label id="result2"/>
 
</mx:WindowedApplication>

I’m interested in anybody else’s ideas or solutions as to how you work with dates across timezones in Flex. Please post a comment.

Return top