Marcos Civil
Marcos Civil

Reputation: 11

Strange file_get_contents behavior

I'm trying to read a file using file_get_contents(), but I get a "failed to open stream' warning when I try to do it without the absolute path.

<?php
    $file = 'C:\wamp\vhosts\testsite.com\a\new.txt'; //works
    $file = '\a\new.txt'; //didn't work
    $file = '/a/new.txt'; //didn't work
    echo file_get_contents($file);

Using WAMP, so there is no permission issues. My question is, what is wrong with using the relative path?

Thanks in advance!

Upvotes: 0

Views: 168

Answers (1)

fejese
fejese

Reputation: 4628

If you start the file name with slashes that is considered an absolute path. So it will be resolved relative to the root directory. In your case '/a/new.txt' will be searched at 'c:/a/new.txt'. To locate files relative to the execution directory loose the leading slash or prefix with a dot: 'a/new.txt' or './a/new.txt'

Upvotes: 1

Related Questions