Services

  • Drupal CMS Developer
  • Website speed optimisation
  • Drupal 6 to Drupal 8 migrations

Welcome

Welcome, I am a Web Developer based in Madrid, Spain originally from the UK. I studied Computer Science & eBusiness at Loughborough University. I specialise in Content Management System websites

Will's Scripts & Functions

Below are some of the snippets of code that I have found useful over the years. Most of them are available and better documented on php.net or respective API sites but I have collected together some of the more common ones that I use or took a long time to find.

 

 

Use the filter form below to filter the scripts and snippets by category...

PHP Listing files in a folder

This script goes through a given folder and finds all of the files and folders within. Using this script it is possible to recursively open up folders.

<?php
$PATH
= $_ENV['DOCUMENT_ROOT'];

$d=$PATH.'/home/server/public_html/folder/';

$files=array(); #initialize array

$dir = opendir($d);
while (
$f = readdir($dir)) {
if (
eregi("\.php",$f)){ #if filename matches .txt in the name
array_push($files,"$f"); #push into $files array
}
}
$count = count($files);
print
":".$d;
for(
$x=0;$x&lt;$count;$x++){

print
"
$x: ".$files[$x];
}
?>

Dynamically read $_POST variables into an array

The code below has been specifically written to take all POST variables and turn them into a string so that they can be used in a URL instead (GET variables)

<?php
foreach ($_POST as $key=>$value)
 
$url .= "&".$key."=" . $value;
?>

Quick and simple Mysql connect

This is the simplest and neatest way to connect to a MySQL database using php

$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database_name");

Dynamic Date ticker/scroller

This is useful for displaying dynamic data in a small space, it scrolls the data and pauses at every line. It will also pause on mouseover

Javascript automatically submit a form

Simply remove the sumit button and place this after the form. Once your browser reads in this script the form will submit.

Note: You must remove the submit button otherwise you will receive a javascript error saying "object doesnt support this function".

<script>
document.form1.submit();
</script>

Simple php email script

This is a simple email script which once uploaded, will send an email everytime the page is requested. It may be a good idea to add some security to this by restricting the destination email to stop spam


<?php
$subject = ''
$destemail = ''
$fromemail = ''
$emailtext = ''

$headers = "From: ".$fromemail." \n";
$headers .= "Reply-To: ".$destemail."\n";
$headers .= "MIME-version: 1.0\n";
$headers .= "Content-type: multipart/alternative; boundary=\"Message-Boundary\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n\n";

Reading in all POST and GET variables

This simple one liner will read in all of the variables sent via POST and GET and turn them into relevant variables within this new page, so $_GET[variable] becomes $variable automatically.

@extract($_POST);
@extract($_GET);

If this doesnt work, and some server setups stop your from doing the above, then you can use a variable variable (yes thats right, you did read it correctly) with the following code

<?php
foreach ($_POST as $key => $value)
$$key = $value;