www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Check Uploaded file is pdf with fileinfo

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    18

    resolved [RESOLVED] Check Uploaded file is pdf with fileinfo

    Hi, I am using the below script to check if the uploaded file is pdf and send it with phpmailer to my email address. Everything works great. It validates and if not pdf it will show the 'PDF file only' message. However, I wanted the message to be in a list like my other errors. How do I achieve that? Thanks.

    PHP Code:
    <?php
        ob_start
    ();
        require(
    "class.phpmailer.php");
        
        
    $errors = array();
        if (
    'POST' === $_SERVER['REQUEST_METHOD'])
        {
            
    $fname           sanitize($_POST['fname']);
            
    $lname           sanitize($_POST['lname']);
            
    $email            sanitize($_POST['email']);
            
            if (empty(
    $fname))
            {
                
    $errors['fname'] = "Please provide first name.";
            }
            if (empty(
    $lname))
            {
                
    $errors['lname'] = "Please provide last name.";
            }
            if (!
    filter_var($emailFILTER_VALIDATE_EMAIL))
            {
                
    $errors['email'] = "Please provide a valid email address.";
            }

            
    $files = array();
            
    $upload $_FILES['upload']['tmp_name'];
            foreach(
    $upload as $uploaded){
              if(!empty(
    $uploaded)) {
            if(isset(
    $uploaded)){
            
    $finfo finfo_open(FILEINFO_MIME_TYPE);
            
    $mimefinfo_file($finfo$uploaded);
            switch(
    $mime) {
                case 
    'application/pdf':
                break;
                default:
                die (
    "PDF file only.");
                  break;
            }}}}
        
            if (
    count($errors) === 0)
            {
            
            
    $message 'some message';
       
            
    $mail = new PHPMailer();

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

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

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

            
    $mail->Send();
            
            
    header("Location: thankyou.php");
               exit();     
        
            }}

    function 
    sanitize($value)
        {
            return 
    trim(strip_tags($value));
        }


    ?>

  2. #2
    Join Date
    Apr 2013
    Posts
    56
    Write this:
    $errors['pdf_error'] = "PDF file only."

    on the line where you have this:
    die ("PDF file only.");

  3. #3
    Join Date
    Mar 2013
    Posts
    18
    That didn't work. It just take me strict to the thankyou page instead of showing the error message.

  4. #4
    Join Date
    Apr 2013
    Posts
    56
    You will have to find out if you even get to default case in switch statement. I'm guessing that you do not, because then the count of $errors variable, would be greater than 0 and you would not be redirected to thankyou page.

  5. #5
    Join Date
    Mar 2013
    Posts
    18
    Thx for the help. I change to use if method since I only have 1 type of file for this script and don't need to use he switch method.

Thread Information

Users Browsing this Thread

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

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