Tuesday, March 1, 2022

[FIXED] PHP writing to file on server

Issue

I'm trying to write to a file on my local server and I can't figure out why, I haven't found any good reason why what I'm doing should not work.

What I've tryed:

file_put_contents("./lol.txt", "Contents");
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/lil.txt", "Contents");
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "lal.txt", "Contents");
file_put_contents("lel.txt", "Contents");

I will provide any additional info if needed.


Solution

Use realpath — Returns canonicalized absolute pathname

file_put_contents(realpath("somedirectory/somefile.txt"),$content);

and check the permission for folder and file whether you have write access.



Answered By - Aditya Shah

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.