Php 在服务器文件夹上上载图像时出现的问题

Php 在服务器文件夹上上载图像时出现的问题,php,file-upload,upload,image-uploading,Php,File Upload,Upload,Image Uploading,我有以下代码在服务器文件夹上传图像,但问题是,即使我上传的是正确格式的图像,它也会给出格式消息 我得到的错误是 Sorry, only JPG, JPEG, PNG & GIF files are allowed 即使图像是jpg/jpeg/png/gif格式 我使用的代码是 <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $target_dir = "profileimg/"

我有以下代码在服务器文件夹上传图像,但问题是,即使我上传的是正确格式的图像,它也会给出格式消息 我得到的错误是

 Sorry, only JPG, JPEG, PNG & GIF files are allowed
即使图像是jpg/jpeg/png/gif格式

我使用的代码是

<?php    
if ($_SERVER["REQUEST_METHOD"] == "POST") 
        {
            $target_dir = "profileimg/";
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            // Check if image file is a actual image or fake image
            if(isset($_POST["submit"])) 
                {
                    if($check !== false) 
                        {
                            //echo "File is an image - " . $check["mime"] . ".";
                            $uploadOk = 1;
                        }
                    else 
                        {
                            echo "File is not an image.";
                            $uploadOk = 0;
                        }
                }

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

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

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) 
                {
                    echo "Sorry, your file was not uploaded.";
                    // if everything is ok, try to upload file
                }
            else 
                {
                    $new_filename = $target_dir . uniqid() . '.' . $imageFileType;
                    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_filename))
                        {   
                            echo $new_filename;

                        }
                }
        }
?>
    <form  role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" enctype="multipart/form-data">
        <input type="file" class="form-control" id="input01" name="fileToUpload" 
        <button>Submit Form</button>
    </form>


您的表单标签需要
enctype=“multipart/form data”>
才能上载文件

所以改变

<form  role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
只检查jpg和all,不检查jpg或jpg

检查你的文件有哪种版本。 放


@Diddle Dot仍然收到相同的错误
print\r($imageFileType)
以查看variable@DiddleDot i在iti添加的enctype中没有得到值,但问题仍然是一样的
 <form  role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" enctype="multipart/form-data">
$allowed = array('gif', 'png', 'jpg', 'jpeg');
$filename = $_FILES['fileToUpload']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
Sorry, only JPG, JPEG, PNG & GIF files are allowed
 strtolower($imageFileType)