Using file_put_contents to create and populate a file in a relatively located folder.
Noticing some odd quirks related to include paths and file creation.
The Short Version: looking at Iteration 2 and Iteration 3. Why does the latter work while the former fails?
Iteration 1
file_put_contents('../../public/remixes/screenshots/test.txt', $data);
This works just fine.
Iteration 2
But, I have the public directory in this module's "root" and want to reference it more directly using pre-set include paths. For example:
echo(get_include_path()); // this outputs "../../"
file_put_contents('public/remixes/screenshots/test.txt', $data, FILE_USE_INCLUDE_PATH);
ERROR: "failed to open stream: No such file or directory"
Iteration 3
Fine, PHP include paths are wonky, so let's do weird things and see what happens. I'll try using fopen to actually create the file first.
fopen('../../public/remixes/screenshots/test.txt', 'w');
file_put_contents('public/remixes/screenshots/test.txt', $data, FILE_USE_INCLUDE_PATH);
That works again... WAT?
Iteration 4
Interesting; I'm OK with doing an fopen first but I want it to use the include path too:
fopen('public/remixes/screenshots/test.txt', 'w', 1);
file_put_contents('public/remixes/screenshots/test.txt', $data, FILE_USE_INCLUDE_PATH);
This yields TWO "failed to open stream" errors. It's getting worse.
Iteration 5
I'll just spare myself the include path "magic" and build the path with it directly
file_put_contents(get_include_path().'public/remixes/screenshots/test.txt', $data);
This works, and is the best I can get.
Conclusion
I'm left wondering why Iteration 2 fails while Iteration 3 succeeds.
Iteration 4 implies that it is a problem with fopen (since file_put_contents is apparently just a wrapper for fopen / fwrite / fclose)
Thoughts?
use_inlude_pathis specified, it "search for the file in the include_path", so it looks like the doc is quite literal-accurate... - Passerby