I want to create a file on the webserver dynamically in PHP.
First I create a directory to store the file. THIS WORKS
// create the users directory and index page $dirToCreate = '..'.$_SESSION['s_USER_URL']; mkdir($dirToCreate, 0777, TRUE); // create the directory for the user
Now I want to create a file called index.php and write out some content into it.
I am trying:
$ourFileName = $_SESSION['s_USER_URL'].'/'.'index.php'; $ourFileHandle = fopen($ourFileName, 'x') or die('can't open file'); fclose($ourFileHandle); // append data to it $ourFileHandle = fopen($ourFileName, 'a') or die('can't write to file'); $stringData = 'Hi'; fwrite($ourFileHandle, $stringData);
But it never gets past the $ourFileHandle = fopen($ourFileName, 'x') or die('can't open file'); Saying the file does not exist, but that is the point. I want to create it.
I did some echoing and the path (/people/jason) exists and I am trying to write to /people/jason/index.php
Does anyone have any thoughts on what I am doing wrong?
PHP 5 on a linux server I believe.
-Jason
First you do :
But the filename you try to write to is not prefixed with the ‘..’, so try changing
to
or probably tidier:
You are probably getting the warning because the directory you are trying to write the file into does not exist