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
Printable View
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
http://us3.php.net/fopen
PHP Code:$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?Quote:
Originally Posted by blm126
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? thanksQuote:
Originally Posted by blm126
You could make a php page with
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.PHP Code:<?
phpinfo();
?>
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 problemQuote:
Originally Posted by Titan85
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
It is disabled. I am going to see more code to find the problem
Do you mean you need some of the script code? Also, could the file to write to being a .txt be a problem?Quote:
Originally Posted by blm126
No. He means he needs all of it.Quote:
Do you mean you need some of the script code?
No.Quote:
Also, could the file to write to being a .txt be a problem?
Ok, here is the full code:
PHP 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>";
?>
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.PHP Code:$sitename = @file_get_contents($filename).$add;
If you want it to append removePHP Code:$sitename = $add;
and replacePHP Code:$sitename = @file_get_contents($filename).$add;
withPHP Code:$file = fopen($filename, "w");
PHP Code:$file = fopen($filename,'a');
Thanks a ton, i got it all working now :)