Sriram
Sriram

Reputation: 10558

Unable to read file in directory with file_get_contents - PHP

I have a file present in a directory content that contains a file to read from. On my MAMP installation, content/ is present in the webroot folder (/Applications/MAMP/htdocs/testApp/content). I am unable to read the file (test.txt) from the folder. The function I am using is:

function getDataFromLibrary($tgt_url) {

        //header('Content-Type: text/html; charset=UTF-8');
        echo "inside get data from lib.";
        if(file_exists($tgt_url)) {
            echo "File does exist.";
            if(is_readable($tgt_url)) {
                echo "file is readable.";
            } else {
                echo "file is not readable.";
            }
        } else {
            echo "File does not exist.";
        }
        echo "tgt url = ".$tgt_url."the end.";
        $file_contents = file_get_contents(urlencode($tgt_url));
        if($file_contents === false) {
            echo "could not read file contents.";
        } else {
            echo "file contents read.";
        }
        echo "Target url=".var_dump($tgt_url)."again the end.";
        echo "File contents = ".$file_contents;
        //$file_contents_replaced = str_replace($file_contents, "\"", "\\\"");
        //echo $file_contents_replaced;
        //var_dump($file_contents);
        //return $tgt_url;
    }

and the output I get when $tgt_url = content/test.txt is:

inside get data from lib.File does exist.file is readable.tgt url = content/test.txtthe end.could not read file contents.string(33) "content/test.txt" Target url=again the end.File contents =  

The file does exist and is also readable. Where am I going wrong?

Any help is most welcome.

Edit:
I turned on the following errors in php.ini: display_errors = On and in the script, I included the lines:

error_reporting(E_ALL);
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);  

The output is:

inside get data from lib.File does exist.file is readable.tgt url = content/test.txtthe end.
Warning: file_get_contents(content%2Ftest.txt): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/testApp/index.php on line 323
could not read file contents.string(33) "content/test.txt" Target url=again the end.File contents =

Upvotes: 1

Views: 1574

Answers (3)

Ganesh
Ganesh

Reputation: 162

Don't use urlencode() inside file_get_contents() functions.

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111859

As you check at the beginning everything $tgt_url you shouldn't after all checks change what you checked. Now you checked if file exists etc for $tgt_url and finally you wanted to get file urlencode($tgt_url) what will be not the same.

Upvotes: 1

dogstar
dogstar

Reputation: 26

After called urlencode(), the $tgt_url will become to "content%2Ftest.txt", not "content/test.txt" again. You can called urlencode() at the front of the getDataFromLibrary(), it will show: File does not exist.

Upvotes: 1

Related Questions