home | contact us
» » July


Handling directories and files with PHP is a snap. However, with this handy function you can always be sure that the destination directory path for your files will exist.

If you pass a path with a filename at teh end, set the second parameter to true eg make_path($path, true)

/*Create  Directory Tree if Not Exists
If you are passing a path with a filename on the end, pass true as the second parameter to snip it off */
function make_path($pathname, $is_filename=false){

	if($is_filename){

		$pathname = substr($pathname, 0, strrpos($pathname, '/'));

	}

    // Check if directory already exists

    if (is_dir($pathname) || empty($pathname)) {

        return true;

    } 

    // Ensure a file does not already exist with the same name

    $pathname = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $pathname);

    if (is_file($pathname)) {

        trigger_error('mkdirr() File exists', E_USER_WARNING);

        return false;

    } 

    // Crawl up the directory tree

    $next_pathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));

    if (make_path($next_pathname, $mode)) {

        if (!file_exists($pathname)) {

            return mkdir($pathname, $mode);

        }

    } 

    return false;

}

 

If you are trying to get a curl script which needs follow on location functionality to run on a server which has either open_basedir or safe mode enabled you will get an error message similar to the following:

CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set

After a bit of digging, some kind soul has put a workaround here

Here is how to use the function


function curl($url){
$go = curl_init($url);
curl_setopt ($go, CURLOPT_URL, $url);
//follow on location problems

if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')){

curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);

$syn = curl_exec($go);

}else{

$syn = curl_redir_exec($go);

}

curl_close($go);
return $syn;
}

//follow on location problems workaround

function curl_redir_exec($ch)

{

static $curl_loops = 0;

static $curl_max_loops = 20;

if ($curl_loops++ >= $curl_max_loops)

{

$curl_loops = 0;

return FALSE;

}

curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);

list($header, $data) = explode("\n\n", $data, 2);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_code == 301 || $http_code == 302)

{

$matches = array();

preg_match('/Location:(.*?)\n/', $header, $matches);

$url = @parse_url(trim(array_pop($matches)));

if (!$url)

{

//couldn't process the url to redirect to

$curl_loops = 0;

return $data;

}

$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));

if (!$url['scheme'])

$url['scheme'] = $last_url['scheme'];

if (!$url['host'])

$url['host'] = $last_url['host'];

if (!$url['path'])

$url['path'] = $last_url['path'];

$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');

curl_setopt($ch, CURLOPT_URL, $new_url);

return curl_redir_exec($ch);

} else {

$curl_loops=0;

return $data;

}

}


 

Creating regular expressions can sometimes be a frustrating experience. Generally I find using regular expressions intuitive and easy, with quick and powerful results. However every now and then you stumble into a regular expression scenario that you just can’t seem to crack.

When this happens you might find yourself trying and trying different regular expressions and then echoing out the results and trying to figure out what is going wrong.

At times like this, a regular expression testing tool is invaluable. If like me though you don’t really like to shell out on something that you are probably hardly ever going to use then this online regular expression tester is great.

Thanks goes out to these guys for hosting such a simple but eminently useful little tool :-)

Further Reading

http://weblogs.asp.net
http://www.imneebu.com/
http://openoffice.blogs.com/
http://www.oreillygmt.co.uk/
http://spellbook.infinitiv.it/
http://www.alexschultz.co.uk/
http://www.colinneller.com/blog/
http://www.embracingchaos.com/
http://www.hanselman.com/blog/
http://www.phpclasses.org/browse/package/3211.html
http://www.phpclasses.org/reviews/id/0596514271.html
http://blogs.oracle.com/


 

Some people like their excel files. For people who want their data exported in an excel format checkout this chunk of code. It’s really easy :-)

First of all you need to download this php excel class

Now try this code:

$query = mysql_query("select * from table");
while($q = mysql_fetch_assoc($query)){
	$output[] = $q;
}
require_once "excel.php";
$export_file = "xlsfile://export.xls";
$fp = fopen($export_file, "wb");
if (!is_resource($fp))
{
    die("Cannot open $export_file");
}
fwrite($fp, serialize($output));
fclose($fp);
header ("Content-Type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=\"exports.xls\"" );
readfile("xlsfile://export.xls");
exit;

That’s got to be the easiest thing you have ever done in PHP. Thanks goes to the excellent PHP Classes site.

Here is some further reading:
PHP Excel Formula Parser
PHP Excel Export class
Export data directly to Excel by configuring the MIME Type Profile Option
Power your PHP Business Logic with Excel
Reading/Parsing Excel Spreadsheet using PHP


 

I have recently completed the second stage of development for my ultimate oscommerce checkout system. This is a replacement for the standard osCommerce checkout which splits into numerous stages, meaning that to actually place an order meant visiting about 5 different pages.

My system rolls every single stage into a single page for entering details and a subsequent confirmation page simply to allow the customer to double check the details.

I have now finished adding full and detailed AJAX error checking so that as the customer fills out the fast checkout page, their input is checked and validated. If their input is OK the input box turns green, otherwise it turns pinky-red and a red error message box displays telling them exactly what the problem is.

This means that it will be nearly impossible for a customer’s checkout process to be complicated by breaking one of the oscommerce character limits or making some other error.

If you want your osCommerce checkout to be as fast and userfriendly as possible then you need to sign up for:

Edmonds Commerce Ultimate osCommerce Checkout

Here are a few screen shots of the system running on a client site.

Existing Customer Login

validates email address and checks that the email address is registered

Existing Customer

Validates Inputs

Validates email addresses to help prevent typos and also checks that double entry of email or password matches up.

validate email

Set Account, Shipping and Billing Address in One Page

The Fast Checkout system automatically defaults the shipping and billing address to the acccount address. If the customer wants to set another address for either of these, an expandable section allows them to enter a new address or select an existing address book entry. Incorporates full AJAX error checking.

shipping billing address

Price

This system can be set up on your site for £997. If you are interested in having the ultimate checkout experience and ensuring that you convert the maximum amount of baskets then get in touch with Edmonds Commerce today.


 
rss icon