file creation failing in php

I am learning file handling in php. I have written the following code to create file:

<?php
    $fileName = "testFile.txt";
    $fileHandle = fopen($fileName,"w") or die ("can't open file");
    fclose($fileHandle);
    phpinfo();
?>

The problem is I am getting "can't open file". I changed the permission of the directory containing this file and all the files in the directory to 777 still the problem persists. Can somebody please help me in solving this issue? Thanks

Upvotes: 1

Views: 91

Answers (3)

bumperbox
bumperbox

Reputation: 10214

have you tried using an absolute path for the $filename

$fileName = "/path/to/file/testFile.txt";

or you can change dir prior to opening the file

chdir('/path/to/file');

http://nz.php.net/manual/en/function.chdir.php

AMENDED ANSWER, TRY THIS

<?php
    $path = "/path/to/file";
    $fileName = "testFile.txt";

    if (! file_exists($path)) {
        die ("$path doesn't exist");
    }

    $fileHandle = fopen("$path/$fileName","w") or die ("can't open file");

    fclose($fileHandle);
    phpinfo();

Upvotes: 1

imm
imm

Reputation: 5919

Try:

$fileHandle = fopen( dirname(__FILE__) . '/' . $fileName , 'w' )  ....

Upvotes: 0

NineAllexis
NineAllexis

Reputation: 52

is the php script and file in the same folder? if not you need to specify the relative path to the testFile.txt

Upvotes: 1

Related Questions