www.webdeveloper.com
Results 1 to 1 of 1

Thread: Email

  1. #1
    Join Date
    Dec 2008
    Posts
    99

    getimagesize validate upload images and send with phpmailer.

    I have a form that will allow upload of multiple image files like below. Only 1 upload is required the rest is optional.

    HTML Code:
    <form method="post" enctype="multipart/form-data" action="upload.php">
    <input type="file" name="photo[]" class="file" />
      <input type="file" name="photo[]" class="file" />
      <input type="file" name="photo[]" class="file" /></form>
    Validate with getimagesize and send with phpmailer.
    PHP Code:
    <?php
    ob_start
    ();
    require(
    "class.phpmailer.php");

    $errors = array();

    if (
    'POST' === $_SERVER['REQUEST_METHOD'])
    {
        
    $firstname           sanitize($_POST['firstname']);
        
    $lastname           sanitize($_POST['lastname']);
        
    $email                 sanitize($_POST['email']);

        if (empty(
    $firstname))
        {
            
    $errors['firstname'] = "Please provide first name.";
        }
        if (empty(
    $lastname))
        {
            
    $errors['lastname'] = "Please provide last name.";
        }
        if (!
    filter_var($emailFILTER_VALIDATE_EMAIL))
        {
            
    $errors['email'] = "Please provide a valid email address.";
        }

        if (
    count($errors) === 0)
        {


    $imageinfo = array();
      
    $my_files $_FILES['photo']['tmp_name'];
      foreach(
    $my_files as $single_file) {
      if(!empty(
    $single_file)) {
      
    $imageinfo[$single_file] = getimagesize($single_file);
      if (
    $single_file['mime'] != 'image/png' && $single_file['mime'] != 'image/jpeg')
      { echo 
    "Invalid Image File";
      exit();
      }  }
      }


    foreach(
    $_FILES['photo']['tmp_name'] as $photo
    if(!empty(
    $photo)) {
    $mail->AddAttachment($photo);


    $message 'some message';

    $mail = new PHPMailer();

    $mail->SetFrom($email);
    $mail->AddAddress($from);

    $mail->Subject  "Submitted";
    $mail->Body     $message;
    $mail->WordWrap 50;
    }

    $mail->Send();

    header("Location: thankyou.php");
    exit();     
    }}

    function 
    sanitize($value)
    {
        return 
    trim(strip_tags($value$problem=''));
    }
    ?>
    My problem is I don't see the invalid image file message when I attached a non image file but I received the email with no attachment either I attach image or non image. Please help. Thanks.
    Last edited by phpnewbie08; 04-09-2013 at 02:34 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles