Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 11th, 2003, 05:11 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem to write file...

in chapter 10 there is a example of simple counter. I made 20 pages reloading , but the counter is always 1 and not increase. What is the problem?...

This is the code:

<?php
$fp = fopen("./cacchio.dat", "r");
if (!$fp) {echo "il file non esiste";}
$contatore = (int) fread ($fp, 20);
fclose($fp);

$contatore++;

echo $contatore;

$fp = fopen("./cacchio.dat", "w");
fwrite ($fp, $contatore);
fclose($fp);
?>

Reply With Quote
  #2 (permalink)  
Old October 11th, 2003, 06:16 PM
richard.york's Avatar
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
Default

Hi joeore,
I haven't really researched your code so this may or may not be the problem. I have noticed in the past when typecasting that the expected output does not always happen when typecasting the result from a function, as is the case in this line:

$contatore = (int) fread ($fp, 20);

I am on a friend's computer at the moment so I don't really have time to dig in and research what else is going on in your code. Try leaving off the typecast and see what transpires.

e.g.:
$contatore = fread($fp, 20);

Admitedly this *may* have nothing to do with the problem.

The other likely problem could be in the write part of the code, verify that you have write permission on the destination directory.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old October 11th, 2003, 08:09 PM
richard.york's Avatar
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
Default

Here's an easy way to find out if you have write permission on that directory:

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

But PHP would probably issue an error or warning if the write was unsuccessful anyway.

In PHP many functions return false if they are unsuccessful in whatever operation is being performed, this is very handy knowledge when you consider adding error reporting to your code.

So far from my research everything looks fine. Other that the typecast used may cause a value other than the expected.

Easy way to find that out:

$contatore = (int) fread ($fp, 20);

echo $contatore." (typecasted)<br />\n";

$contatore = fread ($fp, 20);

echo $contatore." (non-typecasted)<br />\n";

This will give you an answer to that question, provided that the value retrieved from cacchio.dat is a value other than null or zero. Typecasting is completely optional in PHP, the only true benefit from it (IMHO) is getting used to the idea for the sake of learning other languages that do require it.

My *hunch* on this is that the variable's value is being set to zero regardless of the function's result. I bring this up because I ran into a very difficult to find bug in an application I was designing because I had typecast a function's result.

Please let us know if you get it solved!

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old October 12th, 2003, 04:42 AM
richard.york's Avatar
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
Default

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
fclose($fp);

#increment the counter
$contatore++;

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);
fclose($fp);

In deducing the possible errors:
./cacchio.dat
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:
http://www.php.net/chmod

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!";

}

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #5 (permalink)  
Old October 12th, 2003, 07:59 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think that the problem to write file is caused by register_global=off in the file php.ini. Can I resolve this using a super global array $_FILES[]?. How can do this?

Reply With Quote
  #6 (permalink)  
Old October 12th, 2003, 11:21 AM
richard.york's Avatar
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
Default

Quote:
quote:
I think that the problem to write file is caused by register_global=off in the file php.ini. Can I resolve this using a super global array $_FILES[]?. How can do this?
In this case the register_globals directive does not have any effect. The $_FILES superglobal is used and accessed in those cases when you find yourself working with uploaded files, however it has no use at all in this case.

Are the is_writable and fwrite functions returning true or false?
I'm out of suggestions for the time being! Perhaps Nik or another member of the forum will see something that I missed.

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #7 (permalink)  
Old October 12th, 2003, 12:54 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

when I upload the script to my web space, it works correctly. but, when I use my webserver (iis in windows xp home), the script doesn't works correctly and the function fwrite()does not overwrite the file.
If this is a problem ti write permission, how can resolve this?

Reply With Quote
  #8 (permalink)  
Old October 12th, 2003, 02:26 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, typecasting has nothing to do with the problem. The original code simply opens a text file who's sole contents are some number. The script reads up to 20 bytes from the file, more than sufficient to store an average hit counter. The file contents are read in as a string, so the typecast simply converts this to a number. The number is incremented, and written back to the file. When the file is opened for writing, the contents of the file are truncated and writing begins at the first byte.

I would suggest checking the permissions on the file and directory. You might have permissions to read the file but not write to it, etc.

The reason you don't need to worry about the file path is that the script is resonsible for creating the file. If you create a file using a relative path froma specific script (read: working directory), the same relative path will always identify the file in question.

Lastly, $_FILES has nothing to do with accessing any file on the filesystem. $_FILES is a superglobal array that stores data relating to uploaded files. $_FILES doesn't even exist if a user hasn't uploaded a file.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #9 (permalink)  
Old October 12th, 2003, 03:17 PM
richard.york's Avatar
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
Default

Joeore-
Glad to hear you've got it working!
In Windows you navigate to the file (./cacchio.dat) in the file system or the directory that it resides in.
Select "properties" from the right-click menu, and then I believe "sharing", whereas you would grant the access permissions. (Not sure!)

If you find you're getting lost in there then have a look at the plethora of tutorials on Google:
http://www.google.com/search?q=setti...ons+windows+xp

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #10 (permalink)  
Old October 12th, 2003, 05:19 PM
richard.york's Avatar
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
Default

OK I've managed to recreate your problem on my PC.

In Windows XP I was able to get permissions set by navigating to the folder in Windows Explorer (My Computer->Right Click->Explore). Right Click over the directory, select "Properties", then select the "Sharing" tab then check the box "Share this folder on the network" and then check "Allow network users to change my files". This doesn't appear to give the best security.. its a kin to chmod 777.. but so far I haven't been able to find a better way. There's more information on this available from the sharing tab -- click on "Learn more about sharing and security" to open the help dialouge.

Now PHP has write access to the file, if the file doesn't exist, PHP will create it.

I also had a problem with Internet Explorer not updating the cache of the file, which to correct I simply cleared the cache. This is not likely to be a problem if this code is used in a larger application.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write to file angelboy C# 2005 4 March 25th, 2007 05:01 AM
write file abaso.jadhav01 ASP.NET 2.0 Basics 0 November 24th, 2006 06:15 PM
Problem : Write to txt file al-hijjawi ASP.NET 1.0 and 1.1 Basics 0 April 10th, 2006 04:32 AM
program won't write to file CNewbie Visual C++ 1 December 7th, 2004 06:20 PM
reading HTML from file -> write to file mikeuk Beginning PHP 4 July 21st, 2004 05:40 AM



All times are GMT -4. The time now is 05:01 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.