Satch3000
Satch3000

Reputation: 49364

Cannot create a file

I am trying to create a file using php to a dirctory which has cmod 0777 so it should be fine.

Here is the code:

$fh = fopen("/_myfiles/myfile.txt", "w+");

if ($fh==false)
{
    die("unable to create file");
}

But all I get is "unable to create file". Any ideas on what it could be?

Note: For the path I've also tried:

$fh = fopen($_SERVER['DOCUMENT_ROOT']."/_myfiles/myfile.txt", "w+");

with no success.

Upvotes: 0

Views: 5274

Answers (2)

Luke
Luke

Reputation: 23680

fopen() generates an E_WARNING message on failure.

I recommend using error_reporting(E_ALL) to show the warning and this should help you to troubleshoot the problem from there.

Upvotes: 8

Diego
Diego

Reputation: 699

Check write permissions on the directory you want to create the file in.

Also the directory "_myfiles" should exist (it won't be created automatically).

If they are correct, then this will create the file in the same directory where the PHP script is located:

$basedir = dirname(__FILE__);
$fh = fopen($basedir . DIRECTORY_SEPARATOR . 'myfile.txt', 'w+');

Upvotes: 2

Related Questions