Jump to content

[SOLVED] How do I create a fille from a file outside the directory?


jamsearch

Recommended Posts

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";

 

 

 

 

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.