PHP for dummies

The Six rules to be a perfect PHP 5 switcher

It’s so strange to hear about PHP 4, while PHP 5.3 is out there. Probably because there are a lot of frameworks and cms having a PHP 4 fallback.

The first steps you need to know to be a perfect PHP5 switcher, are easy:

  1. Be OOP
    It can be useful read some books, like the most important one Thinking in Java, which is free. Java apart, It explains you the importance to build your library and your works in blocks. You always should try to create your code in small pieces and reusable objects, and use classes and methods instead functions, if you can.
  2. Be DRY
    Every time you write your code, spend a little time to discover if it can be reusable and what you need to do that.
  3. Be organized
    You MUST know about MVC (Model/View/Controller) patterns and Design Patterns. Putting code in the right place it’s more simply than you think
    For example:
    If you are acting on db, you are in Model pattern
    If you are handling user input or you are using fetched data, you are in the Controller pattern
    If you are writing the interface, you are in the View pattern
  4. Don’t feel alone
    Think every time you write your code someone could be working with it:

    • use explanatory names for your methods, classes, functions, variables, constants (ex. do_coffee(), $total_smiles, LIGHT_SPEED) and avoid using numeric variables (ex: $sql1, $sql2…)
    • smart comments on your code. Write the proposal and avoid a simply code translation
    • learn PHPDoc
  5. Be version controlled
    What happens if you lose your code or some changes? Don’t be fool, use Mercurial or Git. Subversion at least. You can have your free repository, if your server hasn’t one.
  6. Be unique but not stupid
    Before writing your code, put down a schema and try to not write your code from scratch. Use frameworks or reusable libraries and help their communities, if you can. Take a look to : Symfony, Zend Framework, Pear

Suggested books:

A 20 rows filebrowser-ftp client script in PHP

Here’s an example of a minimal filebrowser script, similar to a remote ftp client. You can clearly browse files, download them, delete and upload new ones.

<?php
if(isset($_GET['p']) && $_GET['p'])
    if(is_file($_GET['p'])){
        header('Content-Disposition: attachment; filename="'.basename($_GET['p']).'"');
        die(file_get_contents($_GET['p']));
    }else
        chdir(realpath($_GET['p']));
$base_path=getcwd().DIRECTORY_SEPARATOR;
if(isset($_FILES['f']['tmp_name']) && $_FILES['f']['tmp_name'])
    move_uploaded_file($_FILES['f']['tmp_name'], $base_path.$_FILES['f']['name']);
if(isset($_GET['d']) && is_file($base_path.$_GET['d']))
    unlink($base_path.$_GET['d']);
$files=scandir($base_path);
$html_list='';
foreach($files as $file){
    $p=urlencode($base_path.$file);
    $html_list.="<li><a href=\"?p=".$p."\">$file</a> ".((is_file($base_path.$file))?"| <a href=\"?p=$p&d=$file\">delete</a>":"")."</li>";
}
$form_upload='<form action="" method="POST" enctype="multipart/form-data"><input type="file" name="f" /><input type="submit" />';
echo "<h1>".getcwd()."</h1>$form_upload<ul>$html_list</ul>";

And here a screenshot:

Pay attention, this script is not production ready. It is only an experiment to demostrate the minimun work to create a complete file browser script. Put it on a public website could be dangerous.

PHP photo gallery all in one

You have a lot of images and don’t want a fancy Flickr or big framework (ex zenphoto) to show them. You want only put your image folders on an ftp server and show them to the world. Easy as 123. Mininim album could be a one shot solution.

I wrote it several years ago and now I decided to dust and share it because a lot of friends ask me a simple solution to have a nice web gallery of their holiday pictures or a simple photo portfolio.

This script requires only a Linux server and PHP (4 or 5) with obviously  GD library enabled. You don’t have to worry about this requirements. The simplest thing you can do is to put the file on your server and open the page into the browser. If something goes wrong, you will see an error ;)

The install process is simple, because doesn’t exists :)
Download the file from the repository and put the “index.php” file on the server. In the same directory put your image folders.

Ex.:

$ hg clone https://ifabio@bitbucket.org/ifabio/mininim-album/

You can also download the file from the repository as zip.

Use your ftp client (Cyberduck or Filezilla) to upload the file and your images. Remember to put always the images into a separate folder.

Enjoy! You can see a live demo on album.mininim.org

Report a bug.