我只想上传TXT文件,最新消息?

I have problem when I want to upload txt file, the problem is when I want to upload file.txt tell me wrong you must change txt file !! I did the condition when exection != 'txt' not, when ok upload, but what's up guy's!

<?php
$target_dir = "uploads/";
$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"])) {

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    // Allow certain file formats
    if($imageFileType != "txt") {
        echo "Sorry, only txt,  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 {
        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.";
        }
    }
}

?>

Try to get file extension with this:

$ex = explode(".", basename($_FILES["fileToUpload"]["name"]));
$ext = $ex[count($ex) - 1];

But make sure that the uploaded files are not accessible via web browser and are not parsed by your server - the file extension does not say anything about the content and this could be malicious.

change

if($imageFileType != "txt") {

to

 if ($_FILES['file']['type'] == 'text/plain')