jamsearch Posted July 24, 2007 Share Posted July 24, 2007 Hello! Hope someone can help me. I created a new directory on my website using: mkdir('../'.$username.''); All is well there. Now I need a file to be created in that directory named "index.php". I have been trying for days to make this happen but the only way I have learned how to create a file and write to it is only if I run the script within that direcctory. I need to create a file in that directory from one level down (or outside the destination directory in other words). Anyway what I am trying to do is create a new directory with a index.php file in it dynamically. I hope someone understands what I am trying to say. Any help at all would be greatly appreciated. Thanks. Below is what I have been using which works fine inside the directory, it just won't let me create a file outside the directory. $thetext="Text written in the file"; $filename="/hsphere/local/home/witho/xxxxxxxxx.com/index.php"; $tempvar = fopen($filename,"w"); fwrite($tempvar, $thetext); fclose($tempvar); print "The text has been saved"; Link to comment https://forums.phpfreaks.com/topic/61629-solved-how-do-i-create-a-fille-from-a-file-outside-the-directory/ Share on other sites More sharing options...
HaLo2FrEeEk Posted July 24, 2007 Share Posted July 24, 2007 <?php $text = "Text to be written."; $filename = $_SERVER['DOCUMENT_ROOT'].$username.'/index.php'; $handle = fopen($filename, 'w'); if(fwrite($handle, $text)) { echo "The text has been successfully written."; } else { echo "There was an error writing to the file."; } fclose($handle); ?> Try that. It might need some modification for the $filename variable, but I'm sure you can get that. Let us know if you need anything else. Link to comment https://forums.phpfreaks.com/topic/61629-solved-how-do-i-create-a-fille-from-a-file-outside-the-directory/#findComment-306764 Share on other sites More sharing options...
jamsearch Posted July 25, 2007 Author Share Posted July 25, 2007 Thank You very much HaLo2FrEeEk. After playing with it a little and using your code I got it to work perfectly. Link to comment https://forums.phpfreaks.com/topic/61629-solved-how-do-i-create-a-fille-from-a-file-outside-the-directory/#findComment-306808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.