Welcome Guest, Not a member yet? Register   Sign In
weird file_get_contents error
#1

[eluser]dedavai[/eluser]
This error is driving me crazy. When I try to use file_get_contents() I get an "failed to open stream: No such file or directory" error. The strange thing is, the file is sitting in the same directory as the controller. The weirder thing is, file_get_contents() works fine outside the Controllers directory. Has anyone encountered this?
Code:
$a = file_get_contents("test.txt"); // the test.txt is in the controllers directory
#2

[eluser]Michael Wales[/eluser]
Use a relative path from the root.
#3

[eluser]dedavai[/eluser]
Tried that too. Doesn't work. This is my directory structure:

Code:
/opes <- this is where CI index.php is
    /ci-app <-- applications
        /controllers
            - test.php
            - test.txt
    /ci-sys <-- system
None of these work. This is what's driving me crazy.
Code:
$a = file_get_contents("test.txt");
$b = file_get_contents("./test.txt");
$c = file_get_contents("/ci-app/controllers/ci-app/test.txt");
$d = file_get_contents("/opes/ci-app/controllers/ci-app/test.txt");
$e = file_get_contents("/htdocs/opes/ci-app/controllers/ci-app/test.txt");
Can it be a server error? (though it works in other directories).
#4

[eluser]dedavai[/eluser]
file() doesn't work either
#5

[eluser]ELRafael[/eluser]
[quote author="dedavai" date="1190180412"]Tried that too. Doesn't work. This is my directory structure:

Code:
/opes <- this is where CI index.php is
    /ci-app <-- applications
        /controllers
            - test.php
            - test.txt
    /ci-sys <-- system
None of these work. This is what's driving me crazy.
Code:
$a = file_get_contents("test.txt");
$b = file_get_contents("./test.txt");
$c = file_get_contents("/ci-app/controllers/ci-app/test.txt");
$d = file_get_contents("/opes/ci-app/controllers/ci-app/test.txt");
$e = file_get_contents("/htdocs/opes/ci-app/controllers/ci-app/test.txt");
Can it be a server error? (though it works in other directories).[/quote]

hum... what i've done in my env:

i have a public folder, where is all files, outside the system. like /public/javascript, /public/estilos/global.css, /public/images, etc...

So, when i need, i write this:
Code:
$a = file_get_contents('/public/something.txt');

You can use the same idea and create a folder like that.
#6

[eluser]dedavai[/eluser]
Ok, so I figured it out. Any file() or file_get_contents() calls should be made relative to the main Code Igniter index.php (or whatever your index is) REGARDLESS of what directory you're in (controllers, views, models, etc).
#7

[eluser]Derek Allard[/eluser]
Glad you got it figured out! Here's a note from CodeIgniter's File Helper manual page.

Quote:Note: The path is relative to your main site index.php file, NOT your controller or view files. CodeIgniter uses a front controller so paths are always relative to the main site index.

I see you were trying to use native PHP functions instead of CI helpers (which is fine) so for completeness I wanted to add that everything in CI runs through the index.php file, so every call for everything ever gets treated as if it were coming from that file.

Welcome aboard dedavai! Great to have you here.




Theme © iAndrew 2016 - Forum software by © MyBB