1

I am trying to build a text file loader. User can type any word in text input and it will be called $name. I have the text files on my server. But when I type in the words that I don't have, It shows me this.

Warning: file_get_contents(./files/kkk): failed to open stream: No such file or directory

I just trying to ignore the warning. I even try this way. But I am still getting that warning.

if (file_get_contents("./files/".$name,"UTF-8") === false){
        echo'';
        }

This is the code that I have. I am sorry for my english.

<?php
        if(empty($_POST["name"])) {
        echo '';
        }
        else{
        echo '';
        }
        $name =  $_POST["name"]; 
        if (file_get_contents("./files/".$name,"UTF-8") === false){
        echo'';
        }
        else {
        echo "<div id='html1' >";
        echo file_get_contents("./files/".$name,"UTF-8");;
        echo "</div>";
        }
        ?>

Problem was fix using is_file and I removed UTF-8 to use is_file.

Gwa Si
  • 335
  • 1
  • 3
  • 13
  • do you mean you sure you have the file but you are getting the warning ? or you ask about how to handle the warning in case the file is not exist? – Accountant م May 18 '17 at 22:04
  • No, I just want to handle the warning when I type in the words that i don't have. – Gwa Si May 18 '17 at 22:27
  • Possible duplicate of [How can I handle the warning of file_get_contents](http://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-file-get-contents-function-in-php) – Colliot May 18 '17 at 22:32
  • in that case , check if the file exists before you read it, with [is_file](http://php.net/manual/en/function.is-file.php) or [file_exists](http://php.net/manual/en/function.file-exists.php). or if you to just want to stop showing the warning you can use the [Error Control Operators](http://php.net/manual/en/language.operators.errorcontrol.php) just put the at sign "@" before the statement that raise the warning which is `"@file_get_contents"` – Accountant م May 18 '17 at 22:33
  • Possible duplicate of [How can I handle the warning of file\_get\_contents() function in PHP?](http://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-file-get-contents-function-in-php) – Philipp May 18 '17 at 22:36

1 Answers1

2

i suggest using is_file(file)

 <?php
if (empty($_POST["name"])){
    echo '';
}else{
    echo '';
}
$name = $_POST["name"];
if (! is_file("./files/" . $name)){
    echo '';
}else{
    echo "<div id='html1' >";
    echo file_get_contents("./files/" . $name,"UTF-8");
    echo "</div>";
}
?>
  • I got this warning after i did the way you said. `Warning: is_file() expects exactly 1 parameter, 2 given in /home/lisudict/public_html/dic.php on line 38 ` – Gwa Si May 18 '17 at 22:30
  • `is_file` only expects one parameter - you could just remove the `UTF-8` parameter and the code should work – Philipp May 18 '17 at 22:33
  • Yes! I works after I removed UTF-8 on `is_file`. Thank you. The warning is gone.@Philipp – Gwa Si May 18 '17 at 22:59