Web Hosting Talk







View Full Version : PHP 'filesize' ok if ran through ssh, error in web browser


Wojtek
05-25-2009, 01:09 PM
Hello there,

I've moved a simple script from one server to the other and something is making it not work on the new one.

It has to read the filesize of a file and display it.
If I run it through ssh with 'php script.php', the html it outputs is ok and the size is displayed. If however I access the same script.php through an internet browser, it gives me the follosing error:

Warning: filesize() [function.filesize]: stat failed for /var/www/html/asteriskmonitor/logs/Alpha.log in /var/www/html/asteriskmonitor/index.php on line 94

I've tried enabling php safe mode, openbasedir, register globals, chmodding the logs folder and it's files to 777, chowning the folder to apache:apache, but nothing seems to solve the problem.

It must be some kind of permissions issue as I mentioned earlier, root can run it fine through ssh.

Any Ideas?

UNIXy
05-25-2009, 01:44 PM
Have you checked the Apache error_log and suphp.log (if running suphp)? It looks like the error you posted is reported on the browser back to the user and might have been pruned for security reasons.

Regards

Wojtek
05-25-2009, 03:00 PM
httpd's error log reports basically the same thing, but it also gives the IP of the machine opening the page:

[Sun May 24 04:39:06 2009] [error] [client 192.168.15.139] PHP Warning: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for /var/www/html/asteriskmonitor/logs/Alpha.log in /var/www/html/asteriskmonitor/index.php on line 94

That's the only message in the error logs and it repeats over and over


In another forum someone replied that I should just turn off php's eror reporting, but just to clarify here as well, the filesize does not work at all when accesing through the browser. Nothing is output unless I turn error reporting on and in that case I get the warning message.

UNIXy
05-25-2009, 03:09 PM
Is your script and file located in different document roots?

oldunis
05-25-2009, 03:10 PM
That is weird.
Try system("id"); or system("whoami"); to see on what user the web server is running.

Are you able to read the file with php?

Jikson26
05-25-2009, 05:08 PM
Do you have Suhosin installed? It may be the problem. Suhosin is very strict on open base dir configurations.

Wojtek
05-25-2009, 06:03 PM
Is your script and file located in different document roots?

script: /index.php
file: /logs/Alpha.log

both in /var/www/html/asteriskmonitor/

That is weird.
Try system("id"); or system("whoami"); to see on what user the web server is running.

Are you able to read the file with php?

System ID: uid=48(apache) gid=48(apache) groups=48(apache) context=root:system_r:httpd_t:s0 uid=48(apache) gid=48(apache) groups=48(apache) context=root:system_r:httpd_t:s0

System WhoAmI: apache apache

Warning: fopen(/var/www/html/asteriskmonitor/logs/Alpha.log) [function.fopen]: failed to open stream: Permission denied in /var/www/html/asteriskmonitor/index.php on line 27

So it is a permission problem after all...

Do you have Suhosin installed? It may be the problem. Suhosin is very strict on open base dir configurations.

No suhosin installed. It's a freshly installed centos 5.3 box with no extra packages or hosting related apps installed. It's just my dev box.

Wojtek
05-25-2009, 06:35 PM
OK ladies and gentleman, I may have found my problem.

The files I'm trying to read originally come from another server.
For some reason they're owned by some 501 user/group after they've been exported to my dev box through nfs.

-rw-r--r-- 1 501 501 0 May 25 18:25 Alpha.log

even if I chmod 777 and chown root or apache it still cannot read it.
If I however make a new file in that same folder, then I can access the new one through the web browser just fine, even if both have the same permissions:

-rwxr-xr-x 1 root root 1 May 25 18:33 Alpha2.log
-rwxr-xr-x 1 root root 1 May 25 18:33 Alpha.log

Alpha.log is the original that Alpha exported through NFS to my dev box: Unreadable
Alpha2.log is just a file I made in vim.
Both have the same owner/group/permissions yet one is unreadable through the browser...

Wojtek
05-26-2009, 12:34 AM
Got it! ls -Z was the answer

SElinux's permissions caused the problem.
I labeled the files with httpd_sys_content_t and everything works fine now :)