Workshop in Computational Bioskills - Lesson 10

Workshop in Computational Bioskills - Spring 2010

Lesson 10 - PHP Advanced (2)

PHP - short reminder 
Handling File System
Running External Programs
File System
Sub Functions
Sending an eMail
Regular Expressions

PHP - short reminder
PHP - Dynamic Web Pages Language
PHP - programing language, used for writing dynamic web pages,
when the page content depends on the user's input.
Embedded within HTML language

We learned the basic structure of the language:
variables, arrays & control flow
and some more advanced dynamic web programing - Making web forms:
including how to write a web form (in HTML or PHP) and how to process the input.
See the a example for a simple form: simple_form.php
Summarizing important info:


File System Permission code: specifies read/write permissions:
modeactionstarts fromfile does not exist
rbreadfile beginningreturn false
rb+read,writefile beginningreturn false
wbwritefile beginningcreate it
wb+read,writefile beginningcreate it
abwriteend of filecreate it
ab+read,writeend of filecreate it
xbwritefile beginningcreate it, if exists return false
xb+read,writefile beginningcreate it, if exists return false

To check permissions use : file_exists($file_name), is_readable($file_name), is_writable($file_name).
Uploading a file

o Uploading file from the user file system to the server.
o Basically it's just another type of form.
o Possible in CS only under a special secured directory.


     <form action="upload.php" method="post">
     

Choose a file to upload: <input name="userfile" type="file" /> <input type="submit" enctype='multipart/form-data' value="Upload File" /></form>

File upload.php handle the file. See the form, here.
PHP stores all the uploaded file information in the $_FILES autoglobal array.
  • $_FILES['userfile']['name'] The original name of the file on the client machine.
  • $_FILES['userfile']['type'] The mime type of the file, if the browser provided this information.
  • $_FILES['userfile']['size'] The size, in bytes, of the uploaded file.
  • $_FILES['userfile']['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server.
  • $_FILES['userfile']['error'] The error code associated with this file upload.
  • Files will by default be stored in the server's default temporary directory. Use move_uploaded_file() to store an uploaded file permanently.


Running External Programs

o Many things can be written in PHP, but sometimes there is a 
  need to run an external program. 
o This can be done in a similar way to perl, with backticks 
  operator, or with system() or exec() calls. 
o Here in CS, the program must be executed from a safe jailed 
  directory /cs/jailbird/home/.
# returns the command output
$tree = `tree`;
print $tree;


# Does the same but return only the last line of the output.
# $retval is the return value of the command

$tree = system('tree', $retval)
print $tree;



# exec returns the last line of the command output.
# $array argument is filled with the output, where each element is a row.

$output = exec('ls -l', $output_array);
foreach ($output_array as $line)
{
 print $line.'<br>';
}




The File System

As you could have guessed, all the commands required for 
traversing and changing the file system exists. 
chdir, mkdir, chmod, touch, copy, delete, symlink
file_system.php, see the  code

Using sub functions Using sub functions is important in good programing habits, both to avoid code repetition and to work in a top-down design and bottom-up implementation.
This principles are very important here, since in HTML forms in general and in the possible output of a specific form there are many repetitive HTML elements. Use Function to print the repetitive HTML elements!

Example: Writing a form with functions:
//print a submit button function input_submit($element_name, $label) { print '<input type="submit" name="' . $element_name .'" value="'.$label.'"/>'; }
//print a textarea : function input_textarea($element_name) print '<textarea name="' . $element_name . '"</textarea>'; }
// print form: print '<form method="POST" action=" . $_SERVER['PHP_SELF'] . '">'; print '<br/>'; print 'Data: '; input_textarea('description'); print '<br/>'; input_submit('submit', 'Save');


Sending an e-Mail


# mail(to, subject, message[, additional parameters])
# additional parameters are string headers.

$message_body = "this is the body";
mail('bioskill@cs.huji.ac.il','body',
$message_body, 'From: naomih@cs.huji.ac.il');


 mail.php


String manipulations and regular-expressions There are many function on strings in PHP : see the full list here
Some of them we talked about last lesson. Here are some more useful functions for parsing data:
o str_replace( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) :
replaces all occurrences of a substring.
"mixed" : means that the function accepts more than one input type, in this case String or array
(what happens if both search and replace are arrays? not the same size? only one is an array? the subject is an array?)
o preg_replace( string $pattern , string $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) :
This function scans string for matches to pattern , then replaces the matched text with replacement (read more about what will the function do if $pattern is an array, $replacement is an array, what is $limit? $count?).
o More functions on regular expressions ?
Search for preg or ereg in the php documentation and you'll see them all.
Here is an example with pattern matching:
<?php $email = firstname.lastname@aaa.bbb.com; $regexp =
"/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/"; if (preg_match($regexp, $email)) { echo "Email address is valid."; } else { echo "Email address is not valid."; } ?>