knight47 Posted February 4, 2007 Share Posted February 4, 2007 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! Link to comment https://forums.phpfreaks.com/topic/37008-creating-a-file-but-preventing-it-from-overwriting/ Share on other sites More sharing options...
JasonLewis Posted February 4, 2007 Share Posted February 4, 2007 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>'; } Link to comment https://forums.phpfreaks.com/topic/37008-creating-a-file-but-preventing-it-from-overwriting/#findComment-176675 Share on other sites More sharing options...
knight47 Posted February 4, 2007 Author Share Posted February 4, 2007 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! Link to comment https://forums.phpfreaks.com/topic/37008-creating-a-file-but-preventing-it-from-overwriting/#findComment-176679 Share on other sites More sharing options...
knight47 Posted February 4, 2007 Author Share Posted February 4, 2007 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! Link to comment https://forums.phpfreaks.com/topic/37008-creating-a-file-but-preventing-it-from-overwriting/#findComment-176941 Share on other sites More sharing options...
Destruction Posted February 4, 2007 Share Posted February 4, 2007 You don't have echo before the message ie: echo "oops...... Dest Link to comment https://forums.phpfreaks.com/topic/37008-creating-a-file-but-preventing-it-from-overwriting/#findComment-176944 Share on other sites More sharing options...
knight47 Posted February 4, 2007 Author Share Posted February 4, 2007 You don't have echo before the message ie: echo "oops...... Dest LOL! I have horrible eyes, thank you! Link to comment https://forums.phpfreaks.com/topic/37008-creating-a-file-but-preventing-it-from-overwriting/#findComment-176950 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.