【问题标题】:Sorry, only JPG, JPEG, PNG & GIF files are allowed抱歉,只允许使用 JPG、JPEG、PNG 和 GIF 文件
【发布时间】:2017-02-23 16:28:31
【问题描述】:

我正在尝试创建一个脚本,允许用户向我们提供信息并将 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 时,文件扩展名显示错误。

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

【问题讨论】:

  • print_r($imageFileType)。你得到了什么?空吗?
  • @KinshukLahiri 它是空白的......
  • 你确定你在$target_file中的路径是正确的吗?
  • 是的,我认为它会是空白或空的。 print_r($target_file) 给出了什么?
  • $_FILES["fileToUpload"]["name"]的值是多少

标签: php mysql image


【解决方案1】:
$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多