Learning PHP and MySQL

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.

Posted by krautgrrl on 08/19 at 08:22 AM
Chapter 11 Code • (2) Comments • (0) TrackbacksPermalink

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.

Posted by krautgrrl on 08/19 at 08:25 AM
Chapter 11 Code • (20) Comments • (1) TrackbacksPermalink

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.

Posted by krautgrrl on 08/19 at 08:27 AM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

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.

Posted by krautgrrl on 08/19 at 08:34 AM
Chapter 11 Code • (1) Comments • (0) TrackbacksPermalink

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.

Posted by krautgrrl on 08/19 at 08:38 AM
Chapter 11 Code • (3) Comments • (0) TrackbacksPermalink

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.

Posted by krautgrrl on 08/19 at 08:41 AM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

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>

Posted by krautgrrl on 08/19 at 08:42 AM
Chapter 11 Code • (1) Comments • (3922) TrackbacksPermalink

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
}

?>

Posted by krautgrrl on 08/19 at 08:43 AM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

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.
}
?>

Posted by krautgrrl on 08/19 at 08:44 AM
Chapter 11 Code • (2) Comments • (1) TrackbacksPermalink

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']);

Posted by krautgrrl on 08/19 at 08:46 AM
Chapter 11 Code • (1) Comments • (0) TrackbacksPermalink

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>

Posted by krautgrrl on 08/19 at 08:49 AM
Chapter 11 Code • (26) Comments • (2) TrackbacksPermalink

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.

Posted by krautgrrl on 08/19 at 08:52 AM
Chapter 11 Code • (0) Comments • (0) TrackbacksPermalink

Monday, September 25, 2006

Example 12-1 Creating a table from a PHP page in create_table.php

<?php include('db_login.php'); require_once(  'DB.php'  );
$connection  =  DB::connect(  "mysql://$db_username:$db_password@$db_host/
$db_database");
if  (!
$connection)
{
die  ("Could  not  connect  to  the  database:  <br>".  DB::errorMessage());
};
$query  =  '
CREATE  TABLE  `purchases`  (
`purchase_id`  int(11)  NOT  NULL  auto_increment,
`user_id`  varchar(10)  NOT  NULL,
`title_id`  int(11)  NOT  NULL,
`purchased`  timestamp  NOT  NULL, PRIMARY  KEY    (`purchase_id`)
)
'
;
echo  (
"Table  created  successfully!");
$result  =  $connection->query($query);
if  (
DB::isError($result))
{
die  ("Could  not  query  the  database:  <br>".  $query.  "  ".DB::errorMessage($result));
}
$connection
->disconnect();
?>

Posted by krautgrrl on 09/25 at 10:50 AM
Chapter 12 Code • (3) Comments • (75) TrackbacksPermalink

Example 12-10 The delete.php code for performing a delete

<?php require_once('db_login.php'); require_once('DB.php');
$connection  =  DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if  (
DB::isError($connection)){
die  ("Could  not  connect  to  the  database:  <br  />".  DB::errorMessage($connection));
}
$purchase_id  
=  $_GET["purchase_id"];
$query  =  "DELETE  FROM  `purchases`  WHERE  `purchase_id`=$purchase_id";
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could  not  query  the  database:  <br  />".$query."  ".DB::errorMessage($result));
}
?>
<html>
<head>
<title>Item  deleted!</title>
<meta  http-equiv="refresh"  content="4;  url=deletion_link.php">
</head>
<body>
Item  deleted!<br  />
<?php
$query  
=  "SELECT  *  FROM  `purchases`  NATURAL  JOIN  `books`  NATURAL  JOIN  `authors`";
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could  not  query  the  database:  <br  />".$query."  ".DB::errorMessage($result));
}
echo  '<table  border="1">';
echo  
"<tr><th>User</th><th>Title</th><th>Pages</th>";
echo  
"<th>Author</th><th>Purchased</th></tr>";
while  (
$result_row  =  $result->fetchRow(DB_FETCHMODE_ASSOC))  {
echo  "<tr><td>";
echo  
$result_row["user_id"]  .  '</td><td>'; echo  $result_row["title"]  .  '</td><td>'; echo  $result_row["pages"]  .  '</td><td>'; echo  $result_row["author"]  .  "</td><td>";
echo  
$result_row["purchased"]  .  "</td></tr>";
}
echo  "</table>";
$connection->disconnect();
?>
</body>
</html>

Posted by krautgrrl on 09/25 at 11:18 AM
Chapter 12 Code • (4) Comments • (136) TrackbacksPermalink

Example 12-11 Using mysql_insert_id to link up an author to a title

<?php require_once('db_login.php'); require_once('DB.php');
$connection  =  DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if  (
DB::isError($connection)){
die  ("Could  not  connect  to  the  database:  <br  />".  DB::errorMessage($connection));
}
$query  
=  "INSERT  INTO  `books`  VALUES  (NULL,'Python  in  a  Nutshell',600)";
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could  not  query  the  database:  <br  />".$query."  ".DB::errorMessage($result));
}
$last_value  
=  mysql_insert_id();
echo  
"The  id  that  was  created  is:  $last_value<br  />";
$query  =  "INSERT  INTO  `authors`  VALUES  (NULL,$last_value,'Alex  Martelli')";
$result  =  $connection->query($query);
if  (
DB::isError($result)){
die("Could  not  query  the  database:  <br  />".$query."  ".DB::errorMessage($result));
}
echo  "Inserted  successfully!";
$connection->disconnect();
?>

Posted by krautgrrl on 09/25 at 11:18 AM
Chapter 12 Code • (26) Comments • (1) TrackbacksPermalink
Page 10 of 15 pages « First  <  8 9 10 11 12 >  Last »

Statistics

This page has been viewed 407265 times
Page rendered in 2.8529 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: 10
Total anonymous users: 0
Most Recent Visitor on: 05/19/2012 05:08 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine