我只想上传TXT文件,怎么了



我想上传文件时遇到问题txt问题是我想上传file.txt告诉我错误你必须更改txt文件!我在执行时做了条件!='txt'不是,当可以上传时,但是怎么了伙计们!

<?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.";
        }
    }
}
?>

尝试通过以下方式获取文件扩展名:

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

但是请确保上传的文件无法通过Web浏览器访问,并且未被服务器解析 - 文件扩展名不会说明内容,这可能是恶意的。

更改

if($imageFileType != "txt") {

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

相关内容

最新更新