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

时间:2015-05-01 11:11:26

标签: php text upload

我想上传txt文件时遇到问题,问题是当我想上传file.txt告诉我错误时你必须更改txt档!!我在执行时做了条件!='txt'没有,当ok上传时,但是怎么了男人的!

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

?>

2 个答案:

答案 0 :(得分:0)

尝试使用以下方法获取文件扩展名:

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

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

答案 1 :(得分:0)

变化

if($imageFileType != "txt") {

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