Learning PHP and MySQL
Chapter 11 Code
Friday, August 18, 2006
Example 11-17 Adding two days to the date
<?php
$timestamp= time();
echo date("m/d/y G.i:s",$timestamp);
$seconds=2*24*60*60;
$timestamp+=$seconds;
echo "<br>new dates is:";
echo date("m/d/y G.i:s",$timestamp);
?>
This outputs:
12/13/05 16.28:32
new dates is:12/15/05 16.28:32
Chapter 11 Code • (0) Comments • (85) Trackbacks • Permalink
Saturday, August 19, 2006
Example 11-18 Validating two dates
<?php
echo("Validating: 4/31/2005<br>");
if (checkdate(4,31,2005)) {
echo('Date accepted.');
}
else {
echo ('Invalid date.');
}
echo("<br>");
echo("Validating: 5/31/2005<br>");
if (checkdate(5,31,2005)) {
echo('Date accepted.');
}
else {
echo ('Invalid date.');
}
?>
As you can tell by our example in Figure 11-15, the 31 April 2005 date was an invalid date, yet the 31 May 2005 was valid.
Chapter 11 Code • (2) Comments • (0) Trackbacks • Permalink
Example 11-19 Creating a timestamp from the components of a date
<?php
echo("Validating: 5/31/2005<br>");
if (checkdate(5,31,2005)) {
echo('Date accepted: ');
$new_date=mktime(18,05,35,5,31,2005);
echo date("r",$new_date);
}
else {
echo ('Invalid date.');
}
echo("<br>");
?>
This code produces Figure 11-16 when run.
Chapter 11 Code • (20) Comments • (1) Trackbacks • Permalink
Example 11-20 The file_exists.php script checks to see if the file is there
<?php
$file_name="file_exists.php";
if(file_exists($file_name)) {
echo ("$file_name does exist.");
}
else {
echo ("$file_name does not exist.");
}
?>
As you would expect, the file does exist, the output is:
The file exists.php does exist.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-21 Checking the permissions of a file
<?php
$file_name="permissions.php";
if(is_readable($file_name)) {
echo ("The file $file_name is readable.<br>");
}
else {
echo ("The file $file_name is not readable.<br>");
}
if(is_writeable($file_name)) {
echo ("The file $file_name is writeable.<br>");
}
if (is_executable($file_name)) {
echo ("The file $file_name is not writeable.<br>");
}
else {
echo ("The file $file_name is executable.<br>");
}
?>
Our code tells us the many details regarding permissions on the file in Figure 11-17.
Chapter 11 Code • (1) Comments • (0) Trackbacks • Permalink
Example 11-22 Using file_exists, touch, and unlink together
<?php
$file_name="test.txt";
if(file_exists($file_name)) {
echo ("$file_name does exist.<br>");
}
else {
echo ("The file $file_name does not exist.<br>");
touch($file_name);
}
if(file_exists($file_name)) {
echo ("The file $file_name does exist.<br>");
unlink($file_name);
}
else {
echo ("The file $file_name does not exist.<br>");
}
if(file_exists($file_name)) {
echo ("The file $file_name does exist.<br>");
}
else {
echo ("The file $file_name does not exist.<br>");
}
?>
The output looks like Figure 11-18.
Chapter 11 Code • (3) Comments • (0) Trackbacks • Permalink
Example 11-23 Renaming a file
<?php
$file_name="test.txt";
$new_file_name="production.txt";
$status=rename($file_name,$new_file_name);
if ($status) {
echo ("Renamed file.");
}
?>
The file has been renamed, as is demonstrated in the report from Example 11-29. The output is:
Renamed file.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-24 Prompting to upload a file
<html>
<head></head>
<body>
<form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
<br><br>
Choose a file to upload:<br>
<input type="file" name="upload_file">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Chapter 11 Code • (1) Comments • (3922) Trackbacks • Permalink
Example 11-25 Checking for the existence of an uploaded file
<?php
if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'])) {
$error = "You must upload a file!";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
} else {
//proceed to process the file
}
?>
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-26 Checking the file size
Now in Example 11-26, we make sure the file isn’t too big.
<?php
$maxsize=28480;
if ($HTTP_POST_FILES['upload_file']['size'] > $maxfilesize) {
$error = "Error, file must be less than $maxsize bytes.";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
} else {
// proceed to process the file.
}
?>
Chapter 11 Code • (2) Comments • (1) Trackbacks • Permalink
Example 11-27 Checking the file type
Example 11-27 checks the file type to make sure it’s either a JPEG or a GIF file.
<?php
if($HTTP_POST_FILES['upload_file']['type'] != "image/gif" AND
$HTTP_POST_FILES['upload_file']['type'] != "image/pjpeg" AND
$HTTP_POST_FILES['upload_file']['type'] !="image/jpeg") {
$error = "You may only upload .gif or .jpeg files";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
} else {
//the file is the correct format
}
?>
The following line copies the file from the temporary directory into the uploads direc- tory using the same filename.
copy($HTTP_POST_FILES['upload_file']['tmp_name'],"uploads/".
$HTTP_POST_FILES['upload_file']['name']);
Using unlink, let’s remove the temporary file.
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
Chapter 11 Code • (1) Comments • (0) Trackbacks • Permalink
Example 11-28 Processing an uploaded file
<?php
$maxsize=28480; //set the max upload size in bytes
if (!$HTTP_POST_VARS['submit']) {
//print_r($HTTP_POST_FILES);
$error=" ";
//this will cause the rest of the processing to be skipped
//and the upload form displays
}
if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name']) AND
!isset($error)) {
$error = "<b>You must upload a file!</b><br><br>";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if ($HTTP_POST_FILES['upload_file']['size'] > $maxsize AND !isset($error)) {
$error = "<b>Error, file must be less than $maxsize bytes.</b><br><br>";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if($HTTP_POST_FILES['upload_file']['type'] != "image/gif" AND
$HTTP_POST_FILES['upload_file']['type'] != "image/pjpeg" AND
$HTTP_POST_FILES['upload_file']['type'] !="image/jpeg" AND !isset($error)) {
$error = "<b>You may only upload .gif or .jpeg files.</b><br><br>";
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
}
if (!isset($error)) {
copy($HTTP_POST_FILES['upload_file']['tmp_name'],"uploads/".$HTTP_POST_FILES
['upload_file']['name']);
unlink($HTTP_POST_FILES['upload_file']['tmp_name']);
print "Thank you for your upload.";
exit;
}
else
{
echo ("$error");
}
?>
<html>
<head></head>
<body>
<form action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data">
Choose a file to upload:<br>
<input type="file" name="upload_file" size="80">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Chapter 11 Code • (26) Comments • (2) Trackbacks • Permalink
Example 11-29 Executing df and displaying the results
<?php exec("df",$output_lines,$return_value);
echo ("Command returned a value of $return_value.");
echo "</pre>";
foreach ($output_lines as $output) {
echo "$o";
}
echo "</pre>";
?>
For our system, we get the screen in Figure 11-23.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Statistics
This page has been viewed 375174 times
Page rendered in 0.3806 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 8
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:11 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
