Log in

View Full Version : Random increment of a number contained in a text file



KennyP
10-08-2013, 11:25 AM
Hi guys,

is there a PHP script that when it runs it could read a number contained in a text file and then able to increase it randomly by either 1, 2 or 3?

Thanks

Kenny

Beverleyh
10-08-2013, 12:43 PM
$number = file_get_contents('number.txt'); // read number from text file
$random = rand(1,3); // generate random number between 1 and 3
echo $number + $random; // add numbers together and echo on to page

KennyP
10-08-2013, 09:20 PM
$number = file_get_contents('number.txt'); // read number from text file
$random = rand(1,3); // generate random number between 1 and 3
echo $number + $random; // add numbers together and echo on to page

Hi Beverley- thank you very, very much for your reply and the code.

When I run that code as a php file with my browser, it does add a random number to the number it finds in the text page, and it does display the new number in the browser. However, it does not actually write that new number, $number + $random;, back onto the text page. The text page is not locked to any other files, therefore can the code be made to also write the new number onto the text page?

Thanks

KennyP
10-11-2013, 03:28 AM
Hi Beverley- thank you very, very much for your reply and the code.

When I run that code as a php file with my browser, it does add a random number to the number it finds in the text page, and it does display the new number in the browser. However, it does not actually write that new number, $number + $random;, back onto the text page. The text page is not locked to any other files, therefore can the code be made to also write the new number onto the text page?

Thanks

I tried using
print $number + $random; and also tried giving writing permissions to PHP with
$handle = fopen($number, 'w+') ; but I don't know enpough to make it work. Any further help would be very much appreciated.

Thanks

keyboard
10-11-2013, 04:27 AM
Try


$number = file_get_contents('number.txt'); // read number from text file
$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

djr33
10-11-2013, 04:49 AM
What's your full code? You'll need more than just fopen.

Usually you need a combination of:
fopen()
fwrite()
fclose()

Newer versions of PHP may also allow:
file_put_contents()


For a full example, see php.net's documentation for fwrite().


--
Edit: I hadn't noticed keyboard's relatively new post. That's a simple answer, if your version of PHP is new enough (and that function is enabled-- I think sometimes that one might be disabled for security or something).

traq
10-11-2013, 06:09 AM
[file_put_contents is] a simple answer, if your version of PHP is new enough (and that function is enabled-- I think sometimes that one might be disabled for security or something).

There haven't been any changes to it since 5.1, so (I certainly hope!) there are no issues with your version of PHP being new enough.

Hosts usually don't disable it (unless they disable all file writing-related functions), but it may have a directory restriction imposed on it, and it is very likely that stream contexts (e.g., writing a file to a URL, or pipes, etc.) will be disabled.

But none of that should be an issue in this case—if you're allowed to write at all, file_put_contents (http://php.net/file_put_contents) will almost certainly be workable, and is probably the best (most straightforward) solution.

djr33
10-11-2013, 06:32 AM
I don't know exactly what was wrong, but a while ago I couldn't get file_put_contents() to work (it was never clear why), but fopen+fwrite+fclose did the same thing just fine. So I've actually never switched over. Although I only use file_get_contents() when I'm just reading, and that always works.
And all of that was, as far as I know, only PHP 5. I suppose it's possible I was at some point working on a PHP 4 server when it didn't work, but at the time I tried it a few times and never got it to work, so I gave up.

KennyP, if file_put_contents() works for you, that's great, and you should just ignore what I'm saying here :)

KennyP
10-11-2013, 06:51 AM
Thanks very much for your replies guys.

The PHP version is 5.5.32, and there aren't any writing restrictions. Still, the code above is not writing to the text file (set to 777), and now it doesn't even display in the browser as Beverlyh's code. Any suggestions?

djr33
10-11-2013, 07:27 AM
Have you tried the slightly more complicated fwrite() approach? It always works for me.

KennyP
10-11-2013, 07:39 AM
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.

djr33
10-11-2013, 07:45 AM
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:


<?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!
?>


Of course you don't want that extra bit, so just this is fine:


<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fclose($fp);
?>

Just change that to the file/content you want, and that'll be fine.

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:

<?php phpinfo(); ?>
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.

KennyP
10-11-2013, 08:09 AM
Thanks, but how are the following functions translated into that 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

djr33
10-11-2013, 08:13 AM
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:


$fp = fopen('FILENAME.txt', 'w');
fwrite($fp, $random);
fclose($fp);

KennyP
10-11-2013, 08:16 AM
Thanks djr. Nothing I tried works, because I really don't know what I'm doing.

jscheuer1
10-11-2013, 10:25 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:


<?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
?>


The browser cache may need to be cleared and/or the page refreshed to see changes.

If you really must try it with fopen, etc., here it is using that instead:


<?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
?>

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.

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.

KennyP
10-11-2013, 03:58 PM
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...

<?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
?>


It's similar to your code. I'm eager to try yours when I get back later today.

Thanks again

djr33
10-11-2013, 06:39 PM
KennyP, I'm glad it ended up working.


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.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 error :)