Can't create file in PHP

$id = 'pagename'
$file = fopen($id . ".php") or die('An error occurred while creating the page');
$txt = '
<!-- Some HTML code... -->
fwrite($file, $txt);

The script above does not create the file and instead returns “An error occurred while creating the page”.
Is there something wrong with my script? Or is there a restriction that does not allow PHP to create files?

fopen does not create files, it only accesses files that have already been created

See the manual, I think you did something wrong:

PHP: fopen - Manual


No, fopen does create files. My guess on why it’s not working is that they didn’t pass a second parameter.


write attribute on fopen

$fp = fopen($FN, "w");

$file = fopen($id.".php", "w");

$FN = $id . ".php";
$file = fopen($FN, "w");
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.