Jump to content

creating a file, but preventing it from overwriting


knight47

Recommended Posts

I'm wondering if it's possible to create a file, and prevent it to overwrite and give an option to overwrite, or even just an error, for example, this is the code:

 

$file = "$link" . ".htm";
    $create = fopen($file, 'w') or die("Hm... There seems to be a problem, I hope you didn't break anything!");
    fwrite($create, $site_code);
    fclose($create);
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.knight47.com/create' . $link . 'html>';

if I run the same $link twice, it would overwrite the old file, no questions asked, so is it possible to prevent an overwrite, or give an option to overwrite or abort?

 

Thanks!

yep, you can use the file_exists() function.

 


$file = "$link" . ".htm";

if(file_exists($file)){
echo "The file already exists!";
}else{
$create = fopen($file, 'w') or die("Hm... There seems to be a problem, I hope you didn't break anything!");
fwrite($create, $site_code);
fclose($create);
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.knight47.com/create' . $link . 'html>';
}

yep, you can use the file_exists() function.

 


$file = "$link" . ".htm";

if(file_exists($file)){
echo "The file already exists!";
}else{
$create = fopen($file, 'w') or die("Hm... There seems to be a problem, I hope you didn't break anything!");
fwrite($create, $site_code);
fclose($create);
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.knight47.com/create' . $link . 'html>';
}

I LOVE YOU!!!  :-*

 

Thank you very much!

Another small question, I modified it a bit, and so far this is how I have the script setup:

 

$file = $link . ".htm";

if ($_POST['create'] && !file_exists($file))
{
    $create = fopen($file, 'w') or die("Hm... There seems to be a problem, I hope you didn't break anything!");
    fwrite($create, $site_code);
    fclose($create);
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.knight47.com/create/' . $file . '>';
}
else { "oops, i think you have already created a file that already exists, OR your not supposed to be here! GET OUT!!!..."; } 

 

So it will only create it IF the user hits submit, (the submit button is named create), and the file does NOT already exist. So far, I've been testing it, and it's doing it's job, if I create a file that is already there, it does not over write it, but the only problem is that it's not giving the else { } error/message. Do you know why?

 

And also, do you have any idea why it's not being redirected to the new page:

echo '<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.knight47.com/create/' . $file . '>';

is there anything wrong with that?

 

Thanks again!

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.