PHP Sample Code

An Image Upload Script

Here is a handy script that will upload an image from a form that has <input type="file" name="pic" />:

@ extract($_POST);
$filename = $_FILES['pic']['name']; //the actual file name
$temp = $_FILES['pic']['tmp_name']; //the temporary PHP name that's created
if ($temp!="")
{
   $filename_array = explode(".", $filename); //separates the file name string by the "." so we can check the file type
   $type = strtolower($filename_array[count($filename_array)-1]); //make lowercase
   if ($type != "jpg" && $type != "jpeg" && $type != "gif" && $type !="png")
   {
     $error = "wrongFormat";
   }
   else
   {
      $ext = ".jpg";
      $name = "my_new_image".$ext; //creates a new file name called "my_new_image.jpg"
      $newfile = "root/mySite/images/".$name; //creates the whole file location
      @ unlink($newfile);
      @ $result = move_uploaded_file($temp, $newfile); //copies the file to the new location
      if (!$result)
      {
         die("here");
         $error = "unable to move";
      }
   }
}

Submitting Form Results Through Email

So there's this REALLY awesome thing called "PHPMailer" which some people decided to create one day and you can download it at http://phpmailer.sourceforge.net/. THEN once you download it, you should put it in your local folder for your website.

Now, create a new PHP file called "Email.php" and have it require the "class.phpmailer.php" file within the downloaded PHPMailer folder. Here is the Email.php code, commented:

require_once ('phpmailer/class.phpmailer.php');
class Email
{
   var $mailer = ""; //this will be the mailer object
   var $subject = ""; //this is the subject of our email
   var $text = ""; //this is the body of our email, in plain text
   var $fromName = ""; //this is the name of the person sending the email
   var $fromEmail = ""; //this is the email of the person sending the email
   var $toEmail = ""; //this is the email address that we are sending to
   function Email()
   {
      $this->mailer = new PHPMailer();
      $this->mailer->Mailer = "mail";
   }
   function send()
   {
      $this->mailer->Subject = $this->subject; //sets our subject...
      $this->mailer->Body = $this->text; //sets our message body...
      $this->mailer->FromName = $this->fromName; //sets our from name...
      $this->mailer->From = $this->fromEmail; //the email we're sending to...
      $this->mailer->AddAddress($this->toEmail,$this->toName); //add this address...
      if (!$this->mailer->Send()) //SEND, but if it doesn't work, print an error
      {
         trigger_error("Message was not sent. Mailer Error: " . $mail->ErrorInfo, E_USER_WARNING);
      }
      $this->mailer->ClearAddresses();
      $this->mailer->ClearAttachments();
      return true;
   }
}

...and now create a "Mailer.php" file that has all the special email methods like maybe "sendNotificationEmail" or "sendForgottenPassword" etc. All you have to do is include the Email.php file and create a new class called "Mailer" with all your different functions. In each function, create a new Email object:

$mail = new Email();

...and then you can specify all of the email properties like toEmail and subject and message. These can all be parameters for your various functions.

$mail->toEmail="yourname@domain.com";
$mail->send();