Archive

Archive for the ‘PHP’ Category

[PHP] Converting a value to MAC address format

October 20th, 2009
Comments Off

php_codeI just want to post this since someone ask about it. It might help if someone in this world is searching for an answer with the same problem my friend does. This is about converting a value of 0014:f8c4:9e32 or 0014f8c49e32 to 00:14:f8:c4:9e:32

Now here’s some answers for that…

function convertMACAddress($input)
{
    // clean up unwanted characters
    $input = preg_replace( '/[^a-zA-Z0-9]/', '', $input);

    // initiate character positioning
    $pos = 0;

    // declare output container
    $output = '';

     // if given MAC is invalid, terminate process and return an error
     if (strlen($input) < 12)
     {
           return '[Unrecognized MAC address]';
     }

    // initiate convertion...
    for ($i = 0; $i < 6; $i++)
    {
           if ($i == 0)
           {
                  $output .= substr($input, $pos, 2);
           }
           else
           {
                   $output .= ':'.substr($input, $pos, 2);
            }
            $pos += 2;
     }

      return $output;
}

That’s it… Problem solved. :) If need a sample code, please download the source -> Mac Convert (28) — Have fun coding..

Camilo III Info.Tech, PHP, Web Development

[PHP] Shortcuts of If-Else statement

April 5th, 2009
Comments Off

When I learn PHP last September 2005, I was little bit confuse in PHP if-else statement… especially a statement contains with ? and : but somehow, I got it right. I post this cause somebody ask again what’s the difference. So told him, its all the same and its just a shortcuts.

<?php

$authenticated = true;

// [1] standard if-else statement
if ($authenticated)
{
     echo
‘YES’;
}
else
{
      echo ‘NO’;
}

// same with [1] :P
if ($authenticated) { echo ‘YES’; } else { echo ‘NO’; }

// [2] lil bit shortcut
if ($authenticated)
      echo ‘YES’;
else
      echo ‘NO’;

// [3] the shortcut
echo $result = ($authenticated) ? ‘YES’ : ‘NO’;

?>

Or let me say its an evolution of if-else statement… Hahaha.. or maybe the laziness of the programmer. :P So got it? hehehehe..

Camilo III Info.Tech, PHP, Web Development , ,

[PHP] How to upload a file

April 2nd, 2009
Comments Off

I would like to share this script to all since most of my friends ask how to create an upload script using PHP.

Download the PHP script at;

  1. Main Download: Simple Upload Demo (7)
  2. Mirror 1: simple_upload.zip

Upload File Demo: http://www.cable21.net/camilord/simple_upload/index.html

Modification according to your preferred setup

Its default upload directory is uploads/ and to change this, just change the line 19;

// set upload destination
$uploaddir = “uploads/”;

In the script, it is capable to validate allowed file types. As default, images files are only allowed to be uploaded. To change this, just edit the upload.php at line 37.

// allowed file types
$validMIME = array(‘jpg’,‘jpeg’,‘bmp’,‘png’,‘gif’);

Just add any file extension you want to allow… Sample;

// allowed file types
$validMIME = array(‘jpg’,‘jpeg’,‘bmp’,‘png’,‘gif’,‘doc’,‘zip’,‘pdf’);

So I just allowed Document, Zip and PDF files…

That’s all.. I think everything is understandable.. hehehe.. Please read also the comments.

Camilo III Info.Tech, PHP, Web Development , , , , , ,