简体   繁体   中英

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!

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'

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