![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
I need some help with files
I seem to have hit a snag with PHP, if anyone can lend a hand...
This is the code I used: <HTML>
<HEAD>
<TITLE>TESTING PHP</TITLE>
</HEAD>
<BODY>
<?php
$filename = 'test.txt';
$myvalue=0;
if (!$handle = fopen($filename, 'r'))
{ echo "Cannot open file $filename"; exit; }
$myvalue = fscanf ($handle, "%d");
$myvalue++;
fclose($handle);
if (!$handle = fopen($filename, 'w'))
{ echo "Cannot open file $filename"; exit; }
if (fprintf ($handle, "%d", $myvalue) === FALSE)
{ echo "Cannot write to file $filename"; exit; }
echo "Success, wrote $myvalue to file $filename";
fclose($handle);
?>
</BODY>
</HTML>What I want to do is open a file called "test.txt", read the value stored inside (an integer), increment the integer and store it back into the file. It's a kind of counter. The above code gives the following error: Fatal error: Call to undefined function: fprintf() in C:\Program Files\BadBlue\PE\files\test.php on line 21 But the functions printf, fwrite and fread are working and PHP is functional, part for this fprintf thing :eek: |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
change:
[php] if (fprintf ($handle, "%d", $myvalue) === FALSE) [/php] to: [php] if (!fwrite($handle, $myvalue)) [/php]
__________________
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Personally, i think an easier alternative would be...
[php] function incFile($file) { $val = intval(readfile($file)); fopen($file, "w") or die("Could not open \"{$file}\"!"); fwrite($file, $val+1); fclose($file); } incFile("test.txt"); [/php]
__________________
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Posts: 27
Rep Power: 0
![]() |
Thank you, your suggestions seem to work
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|