chapter 11: Practical PHP ==================================== Example: Displaying the same number in different formats ",$value); printf("%b
",$value); printf("%c
",$value); printf("%f
",$value); printf("%o
",$value); printf("%s
",$value); printf("%x
",$value); printf("%X
",$value); ?> ==================================== 42 101010 * 42.000000 52 42 2a 2A ==================================== Example: Displaying a real number in money format ==================================== Please pay $42.00. ==================================== Example: Calculating the length of a string ==================================== Example: Using the word case functions "); echo("$username in lowercase is ".strtolower($username).".
"); echo("$username in first letter uppercase is ".ucwords($username).".
"); ?> ==================================== John Doe in uppercase is JOHN DOE. John Doe in lowercase is john doe. John Doe in first letter uppercase is John Doe. ==================================== Example: Detecting whether a string is contained in another string ==================================== Passwords cannot contain the word "password". ==================================== Example: A simple echo of the timestamp ==================================== Example: Making the date and time appear like we expect ==================================== Example: Adding two days to the date new dates is:"; echo date("m/d/y G.i:s",$timestamp); ?> ==================================== 12/13/05 16.28:32 new dates is:12/15/05 16.28:32 ==================================== Example: Validating two dates "); if (checkdate(4,31,2005)) { echo('Date accepted.'); } else { echo ('Invalid date.'); } echo("
"); echo("Validating: 5/31/2005
"); if (checkdate(5,31,2005)) { echo('Date accepted.'); } else { echo ('Invalid date.'); } ?> ==================================== ==================================== Example: The file_exists.php script checks to see whether the file is there ==================================== ==================================== Example: Checking the permissions of a file "); } else { echo ("The file $file_name is not readable.
"); } if(is_writeable($file_name)) { echo ("The file $file_name is writeable.
"); } else { echo ("The file $file_name is not writeable.
"); } //Only works on Windows with PHP 5.0.0 or later if(is_executable($file_name)) { echo ("The file $file_name is executable.
"); } else { echo ("The file $file_name is not executable.
"); } ?> ==================================== Example: Using file_exists, touch, and unlink together "); } else { echo ("The file $file_name does not exist.
"); touch($file_name); } if(file_exists($file_name)) { echo ("The file $file_name does exist.
"); unlink($file_name); } else { echo ("The file $file_name does not exist.
"); } if(file_exists($file_name)) { echo ("The file $file_name does exist.
"); } else { echo ("The file $file_name does not exist.
"); } ?> ==================================== Example: Renaming a file ==================================== Renamed file. ==================================== Example: Prompting to upload a file


Choose a file to upload:

==================================== $_FILES['upload_file']['name'] ==================================== Example: Checking for the existence of an uploaded file ==================================== Example: Checking the file size $maxfilesize) { $error = "Error, file must be less than $maxsize bytes."; unlink($_FILES['upload_file']['tmp_name']); } else { //Proceed to process the file. } ?> ==================================== Example: Checking the file type ==================================== move_uploaded_file($_FILES['upload_file']['tmp_name'], "uploads/".$_FILES ['upload_file']['name']); ==================================== Example: Processing an uploaded file You must upload a file!

"; unlink($_FILES['upload_file']['tmp_name']); } if ($_FILES['upload_file']['size'] > $maxsize AND !isset($error)) { $error = "Error, file must be less than $maxsize bytes.

"; unlink($_FILES['upload_file']['tmp_name']); } if($_FILES['upload_file']['type'] != "image/gif" AND $_FILES['upload_file']['type'] != "image/pjpeg" AND $_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) { $error = "You may only upload .gif or .jpeg files.

"; unlink($_FILES['upload_file']['tmp_name']); } if (!isset($error)) { move_uploaded_file($_FILES['upload_file']['tmp_name'], "uploads/".$_FILES['upload_file']['name']); print "Thank you for your upload."; exit; } else { echo ("$error"); } ?>
Choose a file to upload:

==================================== ==================