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
if (array_key_exists('_submit_check', $_POST)) {
/* ... process the form ... */
}
mode | action | starts from | file does not exist |
rb | read | file beginning | return false |
rb+ | read,write | file beginning | return false |
wb | write | file beginning | create it |
wb+ | read,write | file beginning | create it |
ab | write | end of file | create it |
ab+ | read,write | end of file | create it |
xb | write | file beginning | create it, if exists return false |
xb+ | read,write | file beginning | create it, if exists return false |
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">File upload.php handle the file. See the form, here.Choose a file to upload: <input name="userfile" type="file" /> <input type="submit" enctype='multipart/form-data' value="Upload File" /></form>
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
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