adamh91 Posted October 25, 2006 Share Posted October 25, 2006 Hey, im trying to remove a string form a file, i tried using replace and replacing it with "", this dosen't work, ive set the chmod to the correct settings aswell, i cant see whats wrong with this.[code]$handle = fopen("usernames.txt","r+");while (!feof($handle)) {$data = fgets($handle); }$data=str_replace($_GET["User"],"",$data);fwrite($handle, $Data);fclose($handle);[/code]thanks ;D Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 25, 2006 Share Posted October 25, 2006 What is the format of the file?Ken Quote Link to comment Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 fopen("[b]usernames.txt[/b]","r+");cant you read ;).txt file :P Quote Link to comment Share on other sites More sharing options...
chiilas Posted October 25, 2006 Share Posted October 25, 2006 are you sure you set the right chmod? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2006 Share Posted October 25, 2006 Now there's a text-book example of how to drive away good help. Quote Link to comment Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 [quote author=Barand link=topic=112700.msg457594#msg457594 date=1161805113]Now there's a text-book example of how to drive away good help.[/quote]Sorry, Was just a joke :( Quote Link to comment Share on other sites More sharing options...
gmwebs Posted October 25, 2006 Share Posted October 25, 2006 fgets() will return *just* a line. You need fread() or better yet, file_get_contents() as it has much better performance. You will then need to decide how to update the text file with your new data.I suggest you [b][i]read[/i][/b] [url=http://uk2.php.net/manual/en/ref.filesystem.php]PHP Filesystem Reference[/url]. (see what I did there? ;))For future reference, always try to debug your variables to see if they are what you expect. Had you echo'ed the output of $data in your script, you would have seen it return just one line. Quote Link to comment Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 Thanks, i echoed the variable to test and it shows it without the string i want to replace, which is good.but how can i write the new data to the file, i tried fwrite and file_put_contents, neither of them work, file_put says function unknown :Sany help?, thanks :D Quote Link to comment Share on other sites More sharing options...
gmwebs Posted October 25, 2006 Share Posted October 25, 2006 Well that is the problem with using unstructured files.Have another look at [url=http://http://uk2.php.net/manual/en/function.file-put-contents.php]file_put_contents[/url] it should accomplish what you need. Provided of course you are using PHP version 5?Something along the lines of...[code]<?phpfile_put_contents($filename, $newdata, LOCK_EX);?>[/code] Quote Link to comment Share on other sites More sharing options...
adamh91 Posted October 25, 2006 Author Share Posted October 25, 2006 PHP version 4.4.1 :-\any other ideas? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.