View Full Version : How to overwrite?
Titan85
09-08-2006, 12:32 AM
I am making a script and i came upon a need to overwrite a file rather than just add to it. I am using $file = @fopen($filename, "w"); but that just adds. Does anyone know the code to overwrite? Thanks for the help
blm126
09-08-2006, 01:40 AM
http://us3.php.net/fopen
$file = @fopen($filename, "w+");
Titan85
09-08-2006, 01:46 AM
http://us3.php.net/fopen
$file = @fopen($filename, "w+");
I actually tried that before and it didn't work. I also just tried it again and still no good :( . By what it says on that page, a simple "w" should work too, but it doesn't. Anyone know whats wrong?
blm126
09-08-2006, 02:17 AM
Your right, "w" should be enough. But "that page" is the PHP manual, and I'm pretty sure it is right sooooo. Make sure
Safe mode is not enabled(or that the PHP user is the owner of the file)
$filename is NOT a URL eg. http://www.blah.com
fopen() is not failing. Remove the "@' in front of fopen to find out
Titan85
09-08-2006, 11:02 AM
Your right, "w" should be enough. But "that page" is the PHP manual, and I'm pretty sure it is right sooooo. Make sure
Safe mode is not enabled(or that the PHP user is the owner of the file)
$filename is NOT a URL eg. http://www.blah.com
fopen() is not failing. Remove the "@' in front of fopen to find out
Ok, i made sure of the second 2 things you said and they check out, but I have cpanel and I am not sure how to check safe mode. Do I just talk to my host? thanks
thetestingsite
09-08-2006, 03:04 PM
You could make a php page with
<?
phpinfo();
?>
and it will say if Safe Mode is enabled. If so, being that you use CPanel, I doubt that your webhost will disable it, but every webhost is different.
blm126
09-08-2006, 07:55 PM
Do I just talk to my host? thanks
Always a good way to find things out, but phpinfo() will tell you. If you could post your complete code I might be able to find the problem
Titan85
09-08-2006, 08:57 PM
Ok, i ran the phpinfo() script and didn't see anything saying safe mode was enabled. Here is the php info http://echodesign.jexhost.com/testing/test.php
blm126
09-09-2006, 03:36 AM
It is disabled. I am going to see more code to find the problem
Titan85
09-09-2006, 04:06 AM
It is disabled. I am going to see more code to find the problemDo you mean you need some of the script code? Also, could the file to write to being a .txt be a problem?
Do you mean you need some of the script code?No. He means he needs all of it.
Also, could the file to write to being a .txt be a problem?No.
Titan85
09-09-2006, 03:03 PM
Ok, here is the full code:
<?php
$filename = "sitename.txt"; // file to write to
$error = "An error occured while changing the site name!";
$success = "Site name changed, redirecting...";
// Change Name
$name = stripslashes($_POST['name']);
// Change URL
if ($sitelink = stripslashes($_POST['sitelink'])) {
$add = "
<!-- Site Name -->
<a href=".$sitelink.">".$name."</a>
";
$sitename = @file_get_contents($filename).$add;
$file = @fopen($filename, "w+");
@fwrite($file, $sitename);
@fclose($file);
$message = $success;
}
else {
$message = $error;
}
echo "
<html>
<head>
<title>Change Site Name</title>
<meta http-equiv=\"refresh\" content=\"2;url=changetitles.php\">
".$message."
</head>
<body>
</body>
</html>";
?>
blm126
09-09-2006, 03:56 PM
$sitename = @file_get_contents($filename).$add;
This line is causing it to append the contents instead of overwriting it. If you want the file overwritten replace the above code with this.
$sitename = $add;
If you want it to append remove
$sitename = @file_get_contents($filename).$add;
and replace
$file = fopen($filename, "w");
with
$file = fopen($filename,'a');
Titan85
09-09-2006, 04:03 PM
Thanks a ton, i got it all working now :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.