Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我只想上传TXT文件,怎么了?_Php_Text_Upload - Fatal编程技术网

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

Php 我只想上传TXT文件,怎么了?,php,text,upload,Php,Text,Upload,当我想上传txt文件时我遇到了问题,问题是当我想上传file.txt告诉我错了你必须更改txt文件!!执行时我做了条件!='txt’不是,上传时可以,但盖伊怎么了 <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINF

当我想上传
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") {


是的,先生,我也没有,同样的问题!我用linux就是那个问题!我在linux中解决了这个问题,因为扩展与windows不一样,谢谢大家
 if ($_FILES['file']['type'] == 'text/plain')