user1853803
user1853803

Reputation: 659

using file_put_contents in php for putting file/content in directory

I am facing a very simple problem in php, while using

 file_put_contents("somefile.txt",$content)

it works but when I try to put file in some directory like-

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

it does'nt work. Any idea what I am missing?

Exact code is-

file_put_contents($_SERVER['DOCUMENT_ROOT']."/temp_code/code.txt",$code);

Upvotes: 1

Views: 4826

Answers (3)

Janith Chinthana
Janith Chinthana

Reputation: 3844

use realpath() as @Amal's answer or use $_SERVER['DOCUMENT_ROOT'] as below,

file_put_contents($_SERVER['DOCUMENT_ROOT']."path/to/your/folder/somefile.txt",$content);

and also care about spellings when programming

sonefile.txt -> somefile.txt

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

Upvotes: 1

internals-in
internals-in

Reputation: 5038

Try 'chmod($dir, 0777); //make it writable'

    $dir="somedirectory";
    if (!is_dir($dir)) 
    {
      mkdir($dir); //create the directory
      chmod($dir, 0777); //make it writable
   }

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

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

Try using an absolute path:

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

Upvotes: 0

Related Questions