Alex87 Posted April 18, 2007 Share Posted April 18, 2007 Hi there, I'm new here, and new to PHP. I've got some programming experience, but I'm totally new to PHP. I was just wondering if someone could help me. I need a script where I basically have a txt file on my server with a number (defaulting to 0), and basically, every time I execute the PHP script, the number in the txt file increments by 1. So in other words, all the php script should do is read the number in the txt and replace the number with number+1. Is that hard to do? If I could get someone to show me how to do this, it would really help a lot. Thanks in advance Alex. Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/ Share on other sites More sharing options...
Glyde Posted April 18, 2007 Share Posted April 18, 2007 <?php $fileToRead = "counter.txt"; $fp = fopen($fileToRead, "w+"); fwrite($fp, intval(fread($fp, 1024)) + 1); fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231840 Share on other sites More sharing options...
jitesh Posted April 18, 2007 Share Posted April 18, 2007 <?php $filename = "your_txt_file.txt"; $handle = fopen($filename, "w+"); $contents = fread($handle, filesize($filename)); $new_no = $contents + 1; fwrite($handle, $new_no); fclose($handle); ?> Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231841 Share on other sites More sharing options...
Alex87 Posted April 18, 2007 Author Share Posted April 18, 2007 Thanks guys, I think I understand how it works. By the way, what does "w+" mean? Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231843 Share on other sites More sharing options...
Glyde Posted April 18, 2007 Share Posted April 18, 2007 w+ opens the file for reading and writing. Simply using w will not allow you to read from the file with that specific file handler. Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231845 Share on other sites More sharing options...
jitesh Posted April 18, 2007 Share Posted April 18, 2007 Table 1. A list of possible modes for fopen() using mode mode Description 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files. 'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files. Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231846 Share on other sites More sharing options...
Alex87 Posted April 18, 2007 Author Share Posted April 18, 2007 Ah okay, makes perfect sense now. So in a nut shell, (ill use the first code as an example) the scripts is doing this: <?php $fileToRead = "counter.txt"; $fp = fopen($fileToRead, "w+"); fwrite($fp, intval(fread($fp, 1024)) + 1); fclose($fp); ?> The PHP header starts Create variable storing the name of the file (for reading and writing) Create a variable for storing the opened file through the file name variable. Then write (to the file, an integer(read( the opened file, no idea what the 1024 means)) + 1 Close the opened file End the PHP script. I'm unsure of what the 1024 stands for. Does it mean that the maximum amount to read is from 0 to 1024 bytes? Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231850 Share on other sites More sharing options...
jitesh Posted April 18, 2007 Share Posted April 18, 2007 $contents = fread($handle, filesize($filename)); ------------------------------------------------------ fread (PHP 3, PHP 4, PHP 5) fread -- Binary-safe file read Description string fread ( resource handle, int length ) fread() reads up to length bytes from the file pointer referenced by handle. Reading stops when up to length bytes have been read, EOF (end of file) is reached, (for network streams) when a packet becomes available, or (after opening userspace stream) when 8192 bytes have been read whichever comes first. Returns the read string or FALSE in case of error. <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); ?> Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231854 Share on other sites More sharing options...
Alex87 Posted April 18, 2007 Author Share Posted April 18, 2007 Ah yes of course. Thanks a lot guys, you have been great help Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-231856 Share on other sites More sharing options...
Glyde Posted April 18, 2007 Share Posted April 18, 2007 I figured a simple counter would never go over 1024 bytes, but hey, what do I know, I guess it could...if you have a lot of page hits. But yea, just replace 1024 with filesize($filename) and it'll work just fine. Link to comment https://forums.phpfreaks.com/topic/47502-simple-script-help-newbie/#findComment-232096 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.