"无效的文件格式"尝试提交表单时出错

时间:2016-02-11 05:09:52

标签: php

当我尝试提交表单时,无法提交表单并显示无效的文件格式"错误,无论是否输入任何数据。文件格式代码是从StackOverflow收集的。

if(isset($_POST['submit']))
{   
    $type=2;
    $fname = @$_POST['f_name'];
    $eml = @$_POST['email_id'];
    $mo_num = @$_POST['mn'];
    $message = @$_POST['message'];
    $uploaded_file_a= @$_FILES['file_a']['name'];
    $uploaded_file_b= @$_FILES['file']['name'];


// in a Array > all Supported Document Formats are Stored //
$allowedExts = array("pdf", "doc", "docx", "ppt", "pptx","jpeg","jpg","png","x-png");

// In a Temporary Array > The File Name + File Extension is Stored //
$temp = explode(".", $_FILES["file"]["name"]);

// Getting the Extension //
$extension = end($temp);
// Checking File // PDF | DOC | DOCX | XLS | XLSX | PPT | PPTX
if ((
   (@$_FILES['file']['type'] == "image/jpeg")
|| (@$_FILES['file']['type'] == "image/jpg") 
|| (@$_FILES['file']['type'] == "image/png") 
|| (@$_FILES['file']['type'] == "image/x-png")
|| (@$_FILES["file"]["type"] == "application/pdf")
|| (@$_FILES["file"]["type"] == "application/msword")
|| (@$_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| (@$_FILES["file"]["type"] == "application/vnd.ms-excel")
|| (@$_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|| (@$_FILES["file"]["type"] == "application/application/vnd.ms-powerpoint")
|| (@$_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.presentationml.presentation"))
&& 
(@$_FILES['file']['error'] == 0)

&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))

{
    $fileName_a = date("YmdHis") . "_" . $_FILES['file_a']['name'];
    $fileName = date("YmdHis") . "_" . $_FILES['file']['name'];

    $valToBind = array(

                        ':F_NM' => $fname,                  
                        ':EM' => $eml,
                        ':MOB' => $mo_num,
                        ':TY' => $type,
                        ':MS' => $message,
                        ':PILEA'=> @$fileName_a,
                        ':PILE'=> @$fileName
                    );
$query = $conn1->prepare("
INSERT INTO `testimonials` (`user_name`,`email`,`phone`,`testimonial_type`,`testimonial_message`,`photo_file`,`image_file`) VALUES
(:F_NM,:EM,:MOB,:TY,:MS,:PILEA,:PILE);
    ");
            $query->execute($valToBind);    
            $rowNumber = $query->rowCount();
            $lastInsertId = $conn1->lastInsertId();
            if($lastInsertId > 0)
                            {
                                {   move_uploaded_file(
        $_FILES['file_a']['tmp_name'], "admin/upload/testimonials/". $fileName_a
                                );}
        {   move_uploaded_file(
        $_FILES['file']['tmp_name'], "admin/upload/testimonials/". $fileName
                                );}
                                echo '<div class="alert alert-success " >
                            Your Testimonials has been sent to us.
                        </div>';

                        }
}   
else
{
    echo'<div class="alert alert-success " > Invalid File Format </div>';
}

        }
        ?>  

<form class="row contact_form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <div class="col-md-12 col-sm-6">                        
        <input type="text" class="form-control" name="f_name" placeholder="Full Name">
    </div>
    <div class="col-md-12 col-sm-6">                        
        <input type="email" class="form-control" name="email_id" placeholder="Email">
    </div>
    <div class="col-md-12 col-sm-6">                        
        <input type="text" class="form-control" name="mn" placeholder="Mobile Number">
    </div>
    <div class="col-md-12 col-sm-6">
    <label> Your Photo</label><br>
         <input type="file" name="file_a" class="form-control" >
    </div>
    <div class="col-md-12 col-sm-6">
    <label> Testimonial in msword, pdf or Image (if scanned)</label><br>
         <input type="file" name="file" class="form-control" >
    </div>
    <div class="col-md-12 col-sm-6">
         <textarea name="message" class="form-control" placeholder="Testimonial Message (if written)"></textarea>
    </div>

    <div class="col-md-12 col-sm-6">
        <input type="submit" name="submit" value="Submit Resume" class="btn btn-primary btn-block" >
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

如果没有提交文件,我已经修改了你要处理的代码。

if(isset($_POST['submit']))
{   
    $type=2;
    $fname = @$_POST['f_name'];
    $eml = @$_POST['email_id'];
    $mo_num = @$_POST['mn'];
    $message = @$_POST['message'];
    $uploaded_file_a= @$_FILES['file_a']['name'];
    $uploaded_file_b= @$_FILES['file']['name'];


// in a Array > all Supported Document Formats are Stored //
$allowedExts = array("pdf", "doc", "docx", "ppt", "pptx","jpeg","jpg","png","x-png");

// In a Temporary Array > The File Name + File Extension is Stored //
$temp = explode(".", $_FILES["file"]["name"]);

// Getting the Extension //
$extension = end($temp);
// Checking File // PDF | DOC | DOCX | XLS | XLSX | PPT | PPTX
if ((
   (@$_FILES['file']['type'] == "image/jpeg")
|| (@$_FILES['file']['type'] == "image/jpg") 
|| (@$_FILES['file']['type'] == "image/png") 
|| (@$_FILES['file']['type'] == "image/x-png")
|| (@$_FILES["file"]["type"] == "application/pdf")
|| (@$_FILES["file"]["type"] == "application/msword")
|| (@$_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| (@$_FILES["file"]["type"] == "application/vnd.ms-excel")
|| (@$_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|| (@$_FILES["file"]["type"] == "application/application/vnd.ms-powerpoint")
|| (@$_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.presentationml.presentation"))
&& 
(@$_FILES['file']['error'] == 0)

&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts) && $_FILES['file']['tmp_name'] != '')

{
    $fileName_a = date("YmdHis") . "_" . $_FILES['file_a']['name'];
    $fileName = date("YmdHis") . "_" . $_FILES['file']['name'];

    $valToBind = array(

                        ':F_NM' => $fname,                  
                        ':EM' => $eml,
                        ':MOB' => $mo_num,
                        ':TY' => $type,
                        ':MS' => $message,
                        ':PILEA'=> @$fileName_a,
                        ':PILE'=> @$fileName
                    );
$query = $conn1->prepare("
INSERT INTO `testimonials` (`user_name`,`email`,`phone`,`testimonial_type`,`testimonial_message`,`photo_file`,`image_file`) VALUES
(:F_NM,:EM,:MOB,:TY,:MS,:PILEA,:PILE);
    ");
            $query->execute($valToBind);    
            $rowNumber = $query->rowCount();
            $lastInsertId = $conn1->lastInsertId();
            if($lastInsertId > 0)
                            {
                                {   move_uploaded_file(
        $_FILES['file_a']['tmp_name'], "admin/upload/testimonials/". $fileName_a
                                );}
        {   move_uploaded_file(
        $_FILES['file']['tmp_name'], "admin/upload/testimonials/". $fileName
                                );}
                                echo '<div class="alert alert-success " >
                            Your Testimonials has been sent to us.
                        </div>';

                        }
}   
else
{
    echo'<div class="alert alert-success " > Invalid File Format </div>';
}

        }
        ?>  

<form class="row contact_form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <div class="col-md-12 col-sm-6">                        
        <input type="text" class="form-control" name="f_name" placeholder="Full Name">
    </div>
    <div class="col-md-12 col-sm-6">                        
        <input type="email" class="form-control" name="email_id" placeholder="Email">
    </div>
    <div class="col-md-12 col-sm-6">                        
        <input type="text" class="form-control" name="mn" placeholder="Mobile Number">
    </div>
    <div class="col-md-12 col-sm-6">
    <label> Your Photo</label><br>
         <input type="file" name="file_a" class="form-control" >
    </div>
    <div class="col-md-12 col-sm-6">
    <label> Testimonial in msword, pdf or Image (if scanned)</label><br>
         <input type="file" name="file" class="form-control" >
    </div>
    <div class="col-md-12 col-sm-6">
         <textarea name="message" class="form-control" placeholder="Testimonial Message (if written)"></textarea>
    </div>

    <div class="col-md-12 col-sm-6">
        <input type="submit" name="submit" value="Submit Resume" class="btn btn-primary btn-block" >
    </div>
</form>

由于 阿米特