对不起,只有JPG,JPEG,PNG&允许GIF文件

时间:2016-10-14 09:33:30

标签: php mysql image

我正在尝试创建一个脚本,允许用户向我们提供信息并将3个图像上传到数据库,以显示他们所面临的问题的证据。

我使用了我在网上找到的脚本并修改了它以使用我的表单。因为我收到错误'抱歉,只有JPG,JPEG,PNG&允许使用GIF文件。这显示在下面的代码中。我上传的图片肯定是.jpg,所以我无法理解为什么会这样?

<?php
    if(isset($_POST['submitForm'])) {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $email = $_POST['email'];
        $organisation = $_POST['organisation'];
        $question_1 = $_POST['question1'];
        $question_2 = $_POST['question2'];
        $question_3 = $_POST['question3'];
        $question_4 = $_POST['question4'];
        $question_5 = $_POST['question5'];
        $image_1 = $_POST['fileToUpload1'];
        $image_2 = $_POST['fileToUpload2'];
        $image_3 = $_POST['fileToUpload3'];
        $message = $_POST['message'];

        $target_dir = "/var/www/vhosts/system/removed.co.uk/etc/contact-facility/uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

        if($check !== false) {
            $uploadOk = 1;
        }

        else {
            $uploadOk = 0;
        }

        if (file_exists($target_file)) {
            $uploadOk = 0;
        }

        if ($_FILES["fileToUpload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }

        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
            $uploadOk = 0;
        }

        $servername = "localhost";
        $username = "removed";
        $password = "removed";
        $dbname = "removed";

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO contact_facility (first_name, last_name, email_address, hospital_trust, question_1, question_2, question_3, question_4, question_5, image_1, image_2, image_3, message) VALUES ('$firstname', '$lastname', '$email', '$organisation', '$question_1', '$question_2', '$question_3', '$question_4', '$question_5', '$image_1', '$image_2', '$image_3', '$message')";
            $conn->exec($sql);

            $success = "<p style='color: green;'>Thank you for contacting REMOVED.<br /><br />We have received your Contact Enquiry and will contact you within 24-48 hours with information regarding your request.</p>";
        }

        catch(PDOException $e) {
            echo $sql . "<br />" . $e->getMessage();
        }

        $conn = null;
    }
?>

我不明白为什么图片是A:没有上传,B:当我上传的图片清晰显示为.jpg时,在文件扩展名上显示错误。

我哪里出错了,你会如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

$target_dir = "uploads/";
        $target_inner = "uploads/".$email;
        $img_inner = mkdir($target_inner);

        if($img_inner=='1'){
            $target_file = $target_inner ."/" . basename($_FILES["fileToUpload"]["name"]);
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        }

         $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
         $uploadOk = 1;

        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "error in uploading image";
        // if everything is ok, try to upload file
        }

        // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;          }
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 100000) {
           echo "Sorry, your file is too large.";
           $uploadOk = 0; 
    }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
            echo "Sorry, only jpg, jpeg and png files are allowed.";
            $uploadOk = 0;

        }

        else
         {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            }
             else {
                echo "Sorry, there was an error uploading your file.";
            }

                if($uploadOk==1)
                {

                    $sql = "INSERT INTO(your query)
// I am sure it will work