检查唯一的电子邮件&将上传文件类型限制为PDF

时间:2015-11-23 04:27:10

标签: php email pdf

我正在尝试制作代码以提交带有电子邮件的表单并附上pdf文件。我已经将数据库中的电子邮件列设置为唯一,但是从前端开始显示文件已上传(尽管未保存记录)。我也在代码中使用了允许的ext,但它不起作用。下面是我正在使用的代码。请求为我修改代码。谢谢。

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

?>

3 个答案:

答案 0 :(得分:1)

替换:

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();
        }

答案 1 :(得分:0)

使用pathinfo获取文件扩展名

$ext = pathinfo($filename, PATHINFO_EXTENSION);

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

对于电子邮件部分,在检查文件扩展名之前,写这样的查询,

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

以上查询的id num行数大于0,然后数据库中已存在电子邮件

检查this是否有唯一的电子邮件部分。

答案 2 :(得分:0)

扩展是点后面的字符。

你可以通过路径名来获取它:

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

然后你允许扩展应该是:

$allowed_ext = 'pdf';

比较如下:

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