简体   繁体   中英

Check for unique e-mail & Restrict upload file type to PDF

I am trying to make a code to submit a form with email and attach a pdf file. I have made e-mail column as unique in the database, but from front end it shows as the file was uploaded(although the record is not saved).I have also used allowed ext in the code, but it doesnt work. below is the code i am using. Request to modify the code for me. Thanks ahead.

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{    
     $name = trim($_POST["uname"]);
     $email = trim($_POST["uemail"]);
     $exp = trim($_POST["uexp"]);
     $desig = trim($_POST["udesig"]);
     $tech = trim($_POST["utech"]); 

    $file = rand(1000,100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $allowed_ext = 'application/pdf';
    $folder="uploads/";


    // new file size in KB
    $new_size = $file_size/1024;  
    // new file size in KB

    // make file name in lower case
    $new_file_name = strtolower($file);
    // make file name in lower case

    $final_file=str_replace(' ','-',$new_file_name);
    if($allowed_ext != $allowed_ext)
            {
                echo "Warning: Please upload your note in PDF file type only";
                unlink($fileTmpLoc);
                exit();
            }
            else

            if(move_uploaded_file($file_loc,$folder.$final_file))

    {

        $sql="INSERT INTO tbl_uploads(name,email,exp,desig,tech,file,type,size) VALUES('$name','$email','$exp','$desig','$tech','$final_file','$file_type','$new_size')";
        mysql_query($sql);
        ?>
        <script>

        window.location.href='success.php';
        </script>
        <?php
    }
    else
    {
        ?>
        <script>
        alert('error while uploading file');
        window.location.href='index.php?fail';
        </script>
        <?php
    }
}

?>

Replace:

if($allowed_ext != $allowed_ext)
        {
            echo "Warning: Please upload your note in PDF file type only";
            unlink($fileTmpLoc);
            exit();
        }

with:

if($file_type != $allowed_ext)
        {
            echo "Warning: Please upload your note in PDF file type only";
            unlink($fileTmpLoc);
            exit();
        }

use pathinfo to get file extentions

$ext = pathinfo($filename, PATHINFO_EXTENSION);

if($ext != "pdf") {
      //error part here
}

for email part, before checking file extension, write query like this,

select * from your_table where email = "{$_POST['email']}".

id num rows of above query is more than 0, then email is already present in your database,

Check this for unique email part.

Extension is the chars after dot.

you can get it by pathname:

$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

and then you allowed extention should be :

$allowed_ext = 'pdf';

compare it like this:

if($allowed_ext == $ext){ ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM