Getting the directory listing using PHP's Glob Function

The other day the new developer  in my office wanted to get a directory listing using  PHP as he was writting a new plugin for WordPress. To his surprise this is really easy with the glob() function:

<?php
foreach (glob("*") as $filename) {
    echo $filename . 'size ' . filesize($filename) . 'n';
}

 

Glob allows you to use patterns similar to those when you are using the ls command in Terminal and returns the file list as an array.

Get the HTML of any page with PHP

Quick tip: if you want to get the HTML for any web page you can use the PHP function file_get_contents(). To see the HTML in your web browser wrap the htmlentities  function around the file_get_contents() function.

<?php

echo htmlentities(file_get_contents('http://example.com'));

Which returns:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>IANA &mdash; Example domains</title>
	<!-- start common-head -->
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="/_css/2008.1/reset-fonts-grids.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="/_css/2008.1/screen.css" />
	<link rel="stylesheet" type="text/css" media="print" href="/_css/2008.1/print.css" />
	<link rel="shortcut icon" type="image/ico" href="/favicon.ico" />
	<script type="text/javascript" src="/_js/prototype.js"></script>
	<script type="text/javascript" src="/_js/corners.js"></script>
	<script type="text/javascript" src="/_js/common.js"></script>
	<!-- end common-head -->

</head>
<body>
	<!-- start common-bodyhead -->
....
	<!-- end common-bodytail -->

</body>
</html>

 

Essential toolkit for a developer building a site

This is my own opinion of the keys to give you a leg up when creating a new PHP/MySQL site:

  1. Apple Mac – Go to Web Directions and it is clear web developers prefer Macs for many reasons (costs vary)
  2. MAMP Pro – So so good, so easy to use and create local hosts on the local in various locations across your Mac ($US59)
  3. Coda – The best IDE / text-editor for Mac – the all in one window is amazing ($US99)
  4. CodeIgniter – Easy to learn and very fast to develop with PHP framework (free)
  5. CodeIgniter Coda Mode – Code hinting for CodeIgniter within Coda (free)
  6. Twitter BootStrap – Basic front end layout (free)
  7. Font Awesome – Font vector images for Twitter BootStrap (free)
  8. Litmus – Get quick screenshots of your sites (from $US49 p/month)
  9. VirtualBox and Windows IE Testing VMs – Microsoft allows you to download VMs to test your site in different versions of IE (free)
  10. Sequel Pro – Awesome database GUI (free)
And a bonus is the amazing Google Apps – GMail, Google Docs, Google Analytics, Google Web Master Tools, Google Maps…(free for low volume usage)

With these tools you can get started on your next site in hours.

Increasing the PHP Memory Limit

Today I needed to run a some code that did a lot of heavy lifting and kept having the error below:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 71 bytes) in /Applications/MAMP/htdocs/test_memory.php on line 46

The solution is to override the memory limit set by PHP.ini:

<?php
ini_set('memory_limit','64M');

Check out PHP.Net’s ini_set documentation for more details.

Notes:

  • This has be done as one of the first commands in your PHP file
  • You may need to have to increase the limit to more if the problem is still occurring
  • If this is unexpected there may be some really inefficient code in your code base

URL Shortening with CodeIgniter

If you are looking for a fast way to URL shorten with CodeIgniter you are in luck.

Basically you set your routes (application->config->routes.php) $route[‘404_override’] to point to the controller and action which directs shorten URLs

e.g. in you routes file:

$route['404_override']='short_links/retrive';

Then short_links has a function retrive

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Short_links extends CI_Controller {

    function retrive()
    {
        $this->load->model('shorten_urls');//load the model which looks after the URLs
        $url = $this->uri->segment(1);//get the URL segment e.g. abc
        redirect($this->shorten_urls->get_url($url)); //lookup the original URL and redirect
    }
}