Thanks for your reply djr. I'd love to try the fwrite() approach, if you would please provide it; I don't have the expertise to write it.
Thanks for your reply djr. I'd love to try the fwrite() approach, if you would please provide it; I don't have the expertise to write it.
As I said, you can get the information on the php.net documentation page (which you should use in general-- it's incredibly useful; I use it all the time).
http://www.php.net/manual/en/function.fwrite.php
There's a lot of information there, so use what you'd like (the one downside to the website is that it can at times appear overly technical, but there's also basic information if you look closely).
This example is the simplest:
Of course you don't want that extra bit, so just this is fine:PHP Code:
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
// the content of 'data.txt' is now 123 and not 23!
?>
Just change that to the file/content you want, and that'll be fine.PHP Code:
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fclose($fp);
?>
Of course I don't know if there still might be some permission issues (that can at times be tricky) or even whether your server disables writing to files entirely. See if that can work.
If it still doesn't work, then try this as a new .php file:
That'll give you information about the configuration and you should be able to see whether it says anything about something like "safe mode" or other reasons that fwrite() might not work.PHP Code:
<?php phpinfo(); ?>
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
KennyP (10-11-2013)
Thanks, but how are the following functions translated into that code?
Code:$random = rand(1,3); // generate random number between 1 and 3 $output = $number + $random; // add numbers together and echo on to page file_put_contents ('number.txt' , $output); //Rewrite file with new data
You should try to play with it to see how the code works. $random is a variable, and you can put that in place of '1' in fwrite:
PHP Code:
$fp = fopen('FILENAME.txt', 'w');
fwrite($fp, $random);
fclose($fp);
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
KennyP (10-11-2013)
Thanks djr. Nothing I tried works, because I really don't know what I'm doing.
Last edited by KennyP; 10-11-2013 at 08:44 AM.
This all seems pretty silly to me - of course I'm only somewhat familiar with PHP. But if file writing privileges are on and the version is what you say it is, you really shouldn't need to bother with anything other than file_put_contents for writing a file. And, if you're worried about the file permissions on the individual file, have PHP create it in the first place. Delete or rename number.txt and use this code:
The browser cache may need to be cleared and/or the page refreshed to see changes.PHP Code:
<?php
@$number = file_get_contents('number.txt') or 0; //open and read number from the file if it exists, or start from 0
$random = rand(1,3); // generate random number between 1 and 3
$output = $number + $random; // add numbers together
file_put_contents ('number.txt' , $output); //write or overwrite file with result
echo $output; //echo the result
?>
If you really must try it with fopen, etc., here it is using that instead:
Again, to be on the safe side remove or rename number.txt to get it out of the way. The code will create its own.PHP Code:
<?php
@$number = file_get_contents('number.txt') or 0; //open and read number from the file if it exists, or start from 0
$random = rand(1,3); // generate random number between 1 and 3
$output = $number + $random; // add numbers together
$fp = fopen('number.txt', 'w'); // w/next 2 lines, write or overwrite file with result
fwrite($fp, $output);
fclose($fp);
echo $output; //echo the result
?>
If neither of these work, it's pretty safe to say that the host does not have file writing privileges turned on. That's different than the setting for the individual file. It's a setting that governs whether or not PHP is allowed to write to any file. It might be something else, but that's the most likely explanation.
Last edited by jscheuer1; 10-11-2013 at 02:21 PM. Reason: add second version
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
KennyP (10-11-2013)
Thank you very much John. I actually stopped in to post the following code that finally worked for me after lots of trial and error...
It's similar to your code. I'm eager to try yours when I get back later today.Code:<?php $number = file_get_contents('test.txt'); // read number from text file $random = rand(1,3); // generate random number between 1 and 3 file_put_contents('test.txt',($number + $random)); // add numbers together and write the total back onto the text file ?>
Thanks again
KennyP, I'm glad it ended up working.
John, that seems like it should be the case (and in fact turned out to be the case here). But for some reason I really did have problems with file_put_contents() a while ago (and still don't use it now, though perhaps after this I'll start trying it again). In the end, it's irrelevant here, but I wasn't sure above if that might be the problem or not. Trial and errorBut if file writing privileges are on and the version is what you say it is, you really shouldn't need to bother with anything other than file_put_contents for writing a file.![]()
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks