NoobScratcher
NoobScratcher

Reputation: 139

How do I use a c:/ directory path in php5 using file_get_contents?

Hello I'm having problems getting my file_get_contents(); to return a string, if I use this function for a file on the actual web server it works fine but if that file which worked on the web server is now moved to my personal computer c:/ drive then file_get_contents(); does not work anymore.... I've tried adding a ini_set(); include path with no avail.

Bit flustered with the lack of absolute accurate documentation on php side unless I've missed something.

I've read the documentation here --> http://www.php.net//manual/en/function.file-get-contents.php and here --> http://www.php.net//manual/en/function.set-include-path.php too.

Here is my code :

<?php
ini_set("auto_detect_line_endings", true);

$db = mysqli_connect("localhost", "root","pass", "DB");

if(mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_errno();
}

$SQL_query = "SELECT text FROM table";

$query = mysqli_query($db, $SQL_query);

echo getcwd();

$file_path = "C:\folder\file.txt";

echo file_get_contents($file_path);

while($result = mysqli_fetch_array($query))
{
    echo $result["username"] . " ";

    if(strstr($file, $result["username"])) 
    {
        echo "Hello this is equal!";

    }else {
        echo"this is not equal!"
    }
}

Upvotes: 1

Views: 2283

Answers (1)

Pred
Pred

Reputation: 9042

Since the backslash is the escape character, you are escaping the f character in that string. The result is C:folderfile.txt.

You have to double the backslash each time you want to use it (with another words, you have to escape the backslash with backslash):

$file_path = "C:\\folder\\file.txt";

chandlermania's suggestion is even better to use the DIRECTORY_SEPARATOR constant

$file_path = "C:" . DIRECTORY_SEPARATOR . "folder" . DIRECTORY_SEPARATOR . "file.txt";

Upvotes: 2

Related Questions