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
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
PHP Code:$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
Focus on Function Web Design
Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps
KennyP (10-11-2013)
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
Last edited by KennyP; 10-11-2013 at 01:37 AM.
Try
PHP Code:$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
KennyP (10-11-2013)
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).
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)
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_contentswill almost certainly be workable, and is probably the best (most straightforward) solution.
KennyP (10-11-2013)
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![]()
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 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?
Have you tried the slightly more complicated fwrite() approach? It always works for me.
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)
Bookmarks