PHP Installer for Web Apps

We all know uploading things via ftp like Squirrel Mail or phpMyAdmin, or any application that has 100′s of small files takes a long time. This is mainly due to the overhead in the commands that have to be preformed to upload each file individually.

The first thing we need to do is to package all of our files into a single large file, this will allow us to massively reduce overhead, since there is only ever one send file command sent. How we do this, is simply add all the files (and the root directory if required) into a zip, so that when they are extracted to the directory on the server, they will appear on the server correctly.

Once we have zipped all of our files up, how do we then expect to be able to unzip them? We need a small bootstrap script (example below) to extract them to the server, which yes, is a second file that is small, but beats having to upload those over few 100 files.

$zip = new ZipArchive();
$r = $zip->open("myzip.zip");
if($r == TRUE) {
  $zip->extractTo("./");
}
$zip->close();

If you now upload and run your bootstrap script, you will notice (if you have the zip extension loaded in PHP) that your zip file has been extracted on the server, and was done a vast amount quicker than it would have taken to upload the individual files.

$package = "myadmin.zip";

$c = file_get_contents($package);

$content = base64_encode($c);
$bcontent = "";

for($i=0; $i<strlen($content); $i+=1024) {
  $c = substr($content, $i, 1024);
  $bcontent .= "\$content .= \"{$c}\";\r\n";
}

$f = fopen("installer.php", "w+");
if($f) {
  fwrite($f, "<?php\r\n");
  fwrite($f, "\$content = \"\";\r\n");
  fwrite($f, $bcontent);
  fwrite($f, "file_put_contents('{$package}', base64_decode(\$content));\r\n");
  fwrite($f, "\$zip = new ZipArchive();\r\n");
  fwrite($f, "\$zip->open('{$package}');\r\n");
  fwrite($f, "\$zip->extractTo('./');\r\n");
  fwrite($f, "\$zip->close();\r\n");
  fwrite($f, "?" . ">");
  fclose($f);
}

Load and Parse Large XML Files in PHP

Usually, PHP is limited to using somewhere between 16mb and 128mb of RAM. So what happens if  you want to parse a 1.1gb file of exported product data (over 500,000 products) and not hit the RAM limiter?

At first this seemed to be a pretty impossible task, as to parse the file you require the entire XML to parse it to a tree.

Usually you would run something such as file_get_contents() and then parse the contents returned, but this would load in the entire 1.1gb of XML and put you well beyond most PHP ram limiters.

What you need to do is parse the XML in small chunks (example  below uses 128kb chunks) and parse those bit by bit, this way, you get to speedily parse through your XML file, while at the same time, steer clear of the PHP RAM limiter.

set_time_limit(0);
define('__BUFFER_SIZE__', 131072);
define('__XML_FILE__', 'pf_1360591.xml');

function elementStart($p, $n, $a) {
  //handle opening of elements
}

function elementEnd($p, $n) {
  //handle closing of elements
}

function elementData($p, $d) {
  //handle cdata in elements
}

$xml = xml_parser_create();

xml_parser_set_option($xml, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);

xml_set_element_handler($xml, 'elementStart', 'elementEnd');
xml_set_character_data_handler($xml, 'elementData');

$f = fopen(__XML_FILE__, 'r');
if($f) {
  while(!feof($f)) {
    $content = fread($f, __BUFFER_SIZE__);

    xml_parse($xml, $content, feof($f));

    unset($content);
  }
  fclose($f);
}

Detect iPhone, Windows Mobile and Other Mobile Browsers

found a nice little piece of php today that allows you to detect practically any mobile browser, they can all be treated the same, or you can redirect different types of browser to different urls.

i recommend downloading the detect mobile browsers code.

the simple usage is:

include('mobile_device_detect.php');
$mobile = mobile_device_detect();

if your a desktop it returns false, if your a mobile browser, it returns true.