Welcome! Log In Create A New Profile

Advanced

[PHP] strange failure

Posted by Jeffry Killen 
Jeffry Killen
[PHP] strange failure
March 22, 2018 01:10AM
Hello;

If this is off topic forgive me.

I have a php script file in a directory that is returning false
for both is_file and is_dir.

It is among a list of files and directories obtained from
scandir. The files are tested in a loop and all other files
in the directory do not have this problem.

There is no evidence of any problem with the file.

Is there any reason that this would happen with and otherwise
normal plain text script file?

built in Apache/php on MacOSX Yosemite.

private function listAsync($_a)
{
// $_ a: array from scandir.
$_owner = array();
$_data = [];
$_out = '';
for($_itr = 0; $_itr < count($_a); $_itr++)
{
$_item = $_a[$_itr];
$_owner = self::getOwner($_item);
if(!isset($_owner['owner']))
{
$_owner['owner'] = "Not determined ".$_owner['error'];
}
$_octPerms = substr(sprintf('%o', @fileperms($_item)), -4);
$_data[count($_data)] .= "owner:".$_owner['owner']."|rwx:".self::getWRXPerms($_item)."|oct:".$_octPerms."|item:".basename($_item);
}
$_out = implode($_data, '{');
return $_out;
}

private function getOwner($_target)
{
$_fileStat = array();
$_errHead = self::$_className." -> ".__FUNCTION__;
if(!is_file($_target) && !is_dir($_target))
{
$_fileStat['error'] = $_errHead." error ".$_target." was not found, or is not a file or directory";
return $_fileStat;
}
$_astat = stat($_target);
$_owner = '';
foreach(posix_getpwuid ( $_astat['uid'] ) as $_sKey=>$_value)
{
if($_sKey == 'name')
{
$_owner = self::$_users[$_value];
}
}
$_fileStat['owner'] = $_owner;
return $_fileStat;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] strange failure
March 22, 2018 03:00AM
On March 22, 2018 12:04:16 AM UTC, Jeffry Killen <[email protected]> wrote:
>Hello;
>
>If this is off topic forgive me.
>
>I have a php script file in a directory that is returning false
>for both is_file and is_dir.
>
>It is among a list of files and directories obtained from
>scandir. The files are tested in a loop and all other files
>in the directory do not have this problem.
>
>There is no evidence of any problem with the file.
>
>Is there any reason that this would happen with and otherwise
>normal plain text script file?
>
>built in Apache/php on MacOSX Yosemite.
>
>private function listAsync($_a)
> {
> // $_ a: array from scandir.
> $_owner = array();
> $_data = [];
> $_out = '';
> for($_itr = 0; $_itr < count($_a); $_itr++)
> {
> $_item = $_a[$_itr];
> $_owner = self::getOwner($_item);
> if(!isset($_owner['owner']))
> {
> $_owner['owner'] = "Not determined ".$_owner['error'];
> }
> $_octPerms = substr(sprintf('%o', @fileperms($_item)), -4);
>$_data[count($_data)] .=
>"owner:".$_owner['owner']."|rwx:".self::getWRXPerms($_item)."|oct:".$_octPerms."|item:".basename($_item);
> }
> $_out = implode($_data, '{');
> return $_out;
> }
>
>private function getOwner($_target)
> {
> $_fileStat = array();
> $_errHead = self::$_className." -> ".__FUNCTION__;
> if(!is_file($_target) && !is_dir($_target))
> {
>$_fileStat['error'] = $_errHead." error ".$_target." was not found, or
>is not a file or directory";
> return $_fileStat;
> }
> $_astat = stat($_target);
> $_owner = '';
> foreach(posix_getpwuid ( $_astat['uid'] ) as $_sKey=>$_value)
> {
> if($_sKey == 'name')
> {
> $_owner = self::$_users[$_value];
> }
> }
> $_fileStat['owner'] = $_owner;
> return $_fileStat;
> }
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php

This might happen if the user that PHP is running as does not have permission to read that file, e.g. it has something like 700 permissions and belongs to 'joe' but php is running as 'www'.
Thanks,
Ash

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] strange failure
March 22, 2018 05:20AM
Thank you for the reply;
I thought I saw the permissions in order. Looking back at it, the owner is ftp user and the perms are
set to 0644 so the server user _www should be able to read it.

The file is a copy of a file elsewhere in the same file system branch. I keep deleting the copy from the
target directory (being read) and then posting a new copy. Each time the same situation occurs. I could
create a seat of the pants script and try to read the directory containing the original file and see if that
show the same or similar symptoms. I suspect it will. I tried changing the name with the same result.
I also noticed that the text encoding was western Latin so I change it to utf-8 with the same result.

This is not a critical issue at present. But it would be helpful to understand if the same or similar
situation occurs in the future.

JK

>>
>
> This might happen if the user that PHP is running as does not have permission to read that file, e.g. it has something like 700 permissions and belongs to 'joe' but php is running as 'www'.
> Thanks,
> Ash


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] strange failure
March 22, 2018 06:00AM
Hurrah: I solved the issue.

As usual it WAS my code.

I was not passing the dir to add to the looping construct
when testing the array item

as in is_file($_arrayItem)
instead of is_file($_dir.'/'.$_arrayItem);

What threw me off is that all accept the problem file were
being processed correctly.

Thanks to all who spent time considering this issue for me.
JK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login