Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 31st, 2003, 11:08 AM
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code Problems

When using the code in Chapter 10 of Beginning PHP4 the part of the code: gets an error

if(!($fp = fopen($counter_file, "r")))

gets an error of:

Warning: fopen("./count.dat", "r") - No such file or directory in /var/www/html/f/test/hit_counter1.php on line 5


I am using PHP 4.2.2. and Apache 2.0.4. This same error occurs with the download version of the same program file. How do I resolve this problem so that I may continue learning how to program?


 
Old December 31st, 2003, 01:39 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Have you created the count.dat file? Using the 'r' mode you are attempting to open the file for reading only, the file must already exist.

Have a look at the PHP manual entry for fopen.
http://www.php.net/fopen

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 31st, 2003, 01:47 PM
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 Yes, I did that, now I get another set of errors

Line 13 says: $fp = fopen($counter_file, "w");
Warning: fopen("./count.dat", "w") - Permission denied in /var/www/html/finazzo/test/hit_counter1.php on line 13

Line 14 says: fwrite($fp, $counter);
Warning: fwrite(): supplied argument is not a valid File-Handle resource in /var/www/html/finazzo/test/hit_counter1.php on line 14

Line 15 says: fclose($fp);
Warning: fclose(): supplied argument is not a valid File-Handle resource in /var/www/html/finazzo/test/hit_counter1.php on line 15


Any Ideas? Is there anything in the php.ini file I need to change?

 
Old December 31st, 2003, 02:16 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You are getting the permission denied error because you haven't set the proper write permissions on the destination directory.

I can walk you through that but first I need to know what OS you are using...
Are you using Windows or Linux? If Windows, what version?

The other two errors are encountered because of the first error.. its a domino effect.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 31st, 2003, 02:18 PM
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using Red Hat 8.0

 
Old December 31st, 2003, 03:04 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

OK, I'm guessing from the look of your file paths that you using Linux. There are a couple of methods for setting permissions.. but all amount to setting the mode setting on the server.

First the easiest and simpliest method:
For setting CHMOD remotely from a Windows Machine.
Download an FTP program that supports changing the CHMOD setting natively. One is WS FTP. Free to download and use, highlight the directory in the viewer and select CHMOD (Unix) from the right click menu.

To set CHMOD via telnet
Open your favorite telnet program. Type in your website's domain name (website must support telnet for this to work). Or if you are using Linux locally, go directy to the command line.

Login with your shell user name and password, typically the same as what you would use for FTP. You should see the welcome message, etc, and then the command line. Now think of the command line in terms of file path hierarchy. You should be now in the same folder as you would be in if you logged in by FTP. You can navigate into the folder heirarchy by typing in CD folder_name. Navigate to the folder you want to change settings on. And then plug in the CHMOD command.

Here is the CHMOD command broken down:

1 = execute
2 = write
4 = read

You add these together to get the desired combination.
There are three groups for which you set permissions for.
Owner / Owner's Group / World at Large

So in short:
chmod 333 - execute write/execute write/execute write
chmod 664 - read write/read write/read
chmod 444 - read only
chmod 222 - write only

I have found in my experience that a setting of chmod 777 has usually been necessary to get PHP scripts working. This of course means, read, write and execute permission for all three groups. The problem arises from the Unix user for PHP which is 'nobody', so in order to grant permission to your PHP script only via chmod 744 or 774 or 644 or 664... one would have to either change the owner for PHP from nobody to that of your shell user name or change the owner of the directory from your shell user name to 'nobody' via the chown command (which many ISP's block).

Be warned however that chmod 777 gives read write and execute permission to anyone who wants to! This is a security hole created.

A third method is to use the PHP function chmod.
This third method usually doesn't work again because PHP's user 'nobody' doesn't match that of your shell user name.

More information is available on the chmod function on the PHP website which even provides a little insight into the command itself:
http://www.php.net/chmod

All that make sense?

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 31st, 2003, 03:39 PM
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Telnet and FTP are not needed for me to access my Linux server, but thank you for that information. I understood chmod is to be made a part of the php "command line" in the program that I write. Since I have not seen this command before (Old Basic Lingo) I added this before my fopen command and got an error message of:

chmod($counter_file, 0777);
Warning: chmod failed: Operation not permitted.

I am getting confused and upset that it is not working. My concern is that with php 4.2.2 and beyond they have switched off certain features such as (register_globals) which I had to switch back on in the php.ini file in order to get the "Post" and "Get" commands to function in earlier chapters. Are there any other php directives turned off that I should be aware of?

Thank you for your help.

 
Old December 31st, 2003, 04:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, when you run chmod() from within PHP, then the user that PHP is running under is the user that's trying to change the permissions level of the file.

If the PHP user doesn't have access to open or read the file to begin with, odds are it won't be able to grant itself access to do so. If that were the case, what good would having permissions be in the first place?

You need to log into the linux account that owns the directory and files you're trying to modify. Rich suggested using WS-FTP to do so, since it's a GUI based tool that allows you to perform simple shell commands like chmod.

If your webserver is your home computer, or if you have a telnet or ssh program on your home computer, then it's easy to just log into your linux web server using the account username and password that you use to write/upload your PHP files.

Go to the directory that your PHP scripts are located.

At the command line, type:

chmod 750 *.php

This sets your permissions to:
Code:
  rwxrwx---
  |||| +-> Group execute
  |||+---> Group read
  ||+----> User  execute
  |+-----> User  write
  +------> User  read
This means that any users in the user-group the file belongs to can read and execute your PHP scripts, but cannot modify or delete them. This also means that no other user on the system can read, write, or execute your PHP files.

Now, the only thing left to do is to change the group of your PHP files. Typically, this is "www" or "nobody" on linux systems.

chown <your_username>:nobody *.php

This keeps you as the file owner, and sets the group to be "nobody". That allows the webserver, via it's group access, to read and execute your php files.


The last thing you need to do is allow the webserver to create new files in your directory. You need to set additional permissions on the directory, similar to how we did for your PHP scripts. Type:

chmod 770 .

This sets full (Read/Write/eXecute) permissions for both you, the user, and the group. We still need to make sure that the webserver has this access via the group. Type:

chown <your_username>:nobody .

That sets the directory's group to be "nobody".


Now, PHP (via the web-server's group), can read and execute existing files in your directory and create new ones.


If you're still having problems, let us know.



Take care,

Nik
http://www.bigaction.org/
 
Old January 2nd, 2004, 10:10 AM
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have done all that you suggested. The code from the book still does not work. I think it is just a bad code and may only work in earlier versions of php. I appreciate your help. I will have to figure this out myself I think.

I have been programming computers on and off for the past 20+ years so I do have an understanding of programming with various languages. I appreciate your help. If I have any other problems I will come back to this forum. Thank you again.

Happy Holidays


 
Old January 2nd, 2004, 05:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, the single line of code you posted isn't "bad". If you suspect that there are other parts of the script bombing, feel free to let us know.

As it stands, the fopen() call should work as long as the webserver user (under which PHP is running) has permission to create and modify files in the directory.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems executing code Neal Access VBA 1 December 11th, 2006 04:07 AM
Problems with the books code...? tarballed BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 9 June 16th, 2004 04:12 PM
Problems in samples code??????? cuccu BOOK: Professional C#, 2nd and 3rd Editions 2 December 4th, 2003 11:52 AM
Problems With New VBA Code Ben Horne Access VBA 4 September 22nd, 2003 07:19 PM
Code Problems sethtrain BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 25th, 2003 10:48 AM





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