简体   繁体   中英

PHP cant read txt file

I have a script that work perfectly with input file except .txt file. I really dunno whats wrong with it. But if i open other extension (like .csv, .rar, .zip, etc) it works and i got something on my $_POST. But if i go with any *.txt file i just get array null on my $_POST.

If i use this code :

$sListText = file_get_contents("act20170713.txt");
echo "<br/>".nl2br($sListText);

Its just work perfectly. But if i use :

$fileexcel = $_FILES['txt']['tmp_name'];
echo $fileexcel;

i got an array null only with *.txt file

Can someone explain it to me or maybe have the solution for it? Any answer will be appreciated. Thank in advance.

Kind regards, Fondra

I add some screenshot for more detail enter image description here

enter image description here

Full code :

<form action="" method="post" enctype="multipart/form-data" name="form2" id="form2"> 
        <div class="row">
        <div class="col-md-3">
        <input id="file" type="file" name="txt" style="text-align:right;-moz-opacity:0;filter:alpha(opacity:0);opacity:1;z-index: 2;"/>     
        </div>
        <div class="col-md-3" style="padding-left:4.5%"><input type="submit" name="list" class="btn btn-info" value="Show"/></div>
        <div class="col-md-3"></div>
        <div class="col-md-3" ><input type="submit" name="clear" class="btn btn-default" value="Clear" style="float:right"/></div>
          </div>
                    </form>
<?php if (isset($_POST['list'])){
        var_dump($_FILES);
        $fileexcel = $_FILES['txt']['tmp_name'];
        echo $fileexcel;
        print_r($_FILES['txt']);

        //echo "test";
        //$sListText = file_get_contents("act20170713.txt");
        //$data = explode("\n", $sListText);
?}

我认为您可以var_dump($_FILEs)进行查看,并检查您的html表单是否具有enctype="multipart/form-data"

This is for PHP 5 on how to read a text file that i found on W3Schools.

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?> 

Source

Hope that helps.

Is the txt file very larger? Perhaps PHP is choking on the file size? Check upload_max_filesize and post_max_size php config settings - I've seen situations where the file exceeds post_max_size did not return any errors, similar to your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM