View Single Post
  #4 (permalink)  
Old October 12th, 2003, 04:42 AM
richard.york's Avatar
richard.york richard.york is offline
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

I was able to get around to doing a few tests on typecasting, my hunch in this case proved to be false.

    function foo()

        $foo = (bool) true;
        return $foo;


    $foo = (int) foo();

    # prints a value of '1'
    echo $foo;

Regardless of whether the value of $foo is cast a string, boolean or integer the typecast of its output prints the correct value. And now giving this some further thought my typecast error happened when I typecast a resource identifier and not a function's output as I first contended.

Reading through your code I can see no other obvious error...

# open cacchio.dat for read only
$fp = fopen("./cacchio.dat", "r");

# if $fp is an empty value, issue file read error
if (!$fp) {echo "il file non esiste";}

# read cacchio.dat up to the first 20 bytes or end of file
$contatore = (int) fread ($fp, 20);

# close the file and purge it from memory

#increment the counter

echo $contatore;

# reopen and truncate cacchio.dat for writing, if cacchio.dat does not exist, create it
$fp = fopen("./cacchio.dat", "w");

# write the incremented value to the file
fwrite($fp, $contatore);

In deducing the possible errors:
may need an absolute path
like: C:\www\files\...
or: home/users/www/etc/

PHP issues warnings if fopen or fwrite are unsuccessful, you would not see the warnings if your error_reporting directive is not set to display them.
But this is unlikely if "il file non esiste" does not output.

The only other likely error that I can deduce is that PHP does not have write permission on the destination directory. If this is the case then you need to set the proper file permissions on that directory, or on the file itself. In Windows this can be done by navigating to the specific file or to its directory and selecting "properties" from the right click menu. In Unix this can be done with the chmod command (via the command line). chmod 664 would likely get things going.

More about chmod here:

If you have questions about how to chmod the file just let us know and someone will walk you through it.

You may also try this:

if (is_writable("./cacchio.dat"))

     if (!fwrite($fp, $contatore))
          echo "Error: unable to write to file!";

} else {

     echo "File is not writable!";


: )

Smiling Souls
Reply With Quote