Nistor Alexandru
Nistor Alexandru

Reputation: 5393

creating text file throws error

I am a php beginner.I want every time when the web page is open to create a file that does not exist. But every time when I run the program I have an error teling me that the file was not created. This is my code:

$ip=$_SERVER["REMOTE_ADDR"];
if(!isset($_COOKIE['firsttime'])){
  setcookie('firsttime', 'no');
  $myfile = 'file/form'.$ip.'.txt';
  if(file_exists($myfile) == FALSE){
    $fo = fopen($myfile, 'w');
    $code = '<form action = "" method = "post">';
    fwrite($fo, $code);
    fclose($fo);
  }else{
    unlink($myfile);
    $file = new File();
  }

}

where is my mistake?

Upvotes: 0

Views: 69

Answers (2)

Floris Velleman
Floris Velleman

Reputation: 4888

Not completely sure but that would result in a pretty weird file name.

$myfile = 'file/form'.$ip.'.txt';

If my ip is 1.0.0.01.23 (really random and pretty weird) the file name would be:

file/form.1.0.0.01.23..txt

Try saving a file with that name in notepad.

Upvotes: 0

user1943931
user1943931

Reputation:

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

Do this to open a file, and it will create the file if it doesn't exist.

Upvotes: 2

Related Questions