Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if network file exists

Status
Not open for further replies.

rhyno2k

IS-IT--Management
Jun 9, 2001
222
0
0
US
I've been racking my brains on (and Googling, Tek-Tipping and PHP.Netting) this for hours...

I'm trying to check if a file exists on a network drive:
Code:
$filename = '//otherserver/dir/knownfile.ext';
if (file_exists($filename)) {
   echo ... // etc.
}

(It works for local files) e.g.
Code:
$filename = 'c:/pkgclnup.log';

The PHP manual says you should be able to do this. I've tried forward slashes, backslashes, escaped backslashes, "file:" prefixes, even mapping a network drive and referencing the drive letter... nothing works.

I'm running PHP 5 on IIS 6 (Win Server 2003).

The file definitely exists, and can be gotten to in various ways (UNC, mapped drive, etc.) from Start | Run on the web server.

I'm not running in safe mode, and open_basedir is not set.

Any ideas? I'm not married to this particular function, just anything that will see if a file exists on the network.


Thanks,
--RHYNO
 
I don't know what to tell you.

Running PHP 5.1.4 and 5.0.5 from the command-line, the following code:

Code:
<?php
$filename = '//[URL unfurl="true"]www/http/scan.jpg';[/URL]

if (file_exists($filename))
{
	print "It's there!";
}
else
{
	print "It's not there!";
}
?>

works as advertised.



Want the best answers? Ask the best questions! TANSTAAFL!
 
does the IUSR_ account that you are running php under have browse and read permissions on the remote directory?

if you change the file_exists call to an fopen one you might be able to diagnose the issue. when i tried i (as expected) got loads of permission denied errors.
 
curiously, with the right permissions this code
Code:
$paths[] = "//daneel/Photos/DCIM/100KC340/100_0158.JPG";
$paths[] = "file://daneel/Photos/DCIM/100KC340/100_0158.JPG";
$paths[] = "\\\\daneel\\Photos\\DCIM\\100KC340\\100_0158.JPG";
clearstatcache();
foreach ($paths as $path):
 if (file_exists($path)):
  echo "True<br/>"; 
 else: 
  echo  "false<br/>";
 endif;
endforeach;

gives me this result
True
false
True

I would have expected trues across the board.
 
Hi guys,

"Everyone" has read & execute permissions on the remote directory. I tried using fopen and got the following...

When using mapped drive letter ($filename="S:/..."): "failed to open stream: No such file or directory"

When using UNC path ($filename="//server/..."): "failed to open stream: Invalid argument"

(both on the fopen line)
 
if you are running win 2003 then anonymous login users are not, by default, members of the EVERYONE group.

to be explicit you will need to give the anonymous login group access rights to the directory. or put the page you are accessing behind some IIS security (windows authentication perhaps, disable anonymous access)

i can not use mapped drives with stat() functions. i am not surprised that they don't work for you.

and lastly i would guess the fopen call is failing on the unc path because you have not used a valid argument ;-) try fopen($filename, "rb")

 
J,

I tried both of your suggestions:

Code:
<?php
echo "<h2>file_exists</h2>";

$paths[] =  'c:/a.txt';
$paths[] =  's:/pictures/a.txt';
$paths[] =  "//myserver/company/solidworks/pictures/a.txt";
$paths[] =  "\\\\myserver\\company\\solidworks\\pictures\\a.txt";
clearstatcache();
foreach ($paths as $path):
 if (file_exists($path)):
  echo "$path: <font color=green>true</font><br/>";
 else:
  echo  "$path: <font color=red>false</font><br/>";
 endif;
endforeach;

echo "<h2>fopen</h2>";

foreach ($paths as $path):
 if (fopen($path,"r")):
  echo "$path: <font color=green>true</font><br/>";
 else:
  echo  "$path: <font color=red>false</font><br/>";
 endif;
endforeach;
?>

...only the local C: file registers "true".

I added Read/Exe permissions to "ANONYMOUS LOGIN". Each of the file references works fine on command line and on Start | Run...
 
what errors are you getting at fopen?

sorry - my fault this (i've actually been using file_exists for my tests: i advocated fopen just so you could see the permissions issue) - the if statement won't work as you intend as if fopen works it will provide a resource handle rather than a boolean true. invert the test instead:
Code:
 if (!fopen($path,"r")):  
  echo "$path: <font color=red>false</font><br/>";
 else:
  echo  "$path: <font color=green>true</font><br/>";
 endif;

 
J,

Getting this on fopen (using the inverted test):


Warning: fopen(//server/company/solidworks/pictures/a.txt) [function.fopen]: failed to open stream: Invalid argument in D:\Web\Sites\...\file.php on line 20
//server/company/solidworks/pictures/a.txt: false



Line 20 is the fopen line (if (!fopen($path,"r")):)
 
just in case i'm being very dim...
I added Read/Exe permissions to "ANONYMOUS LOGIN". Each of the file references works fine on command line and on Start | Run...
you did add these permissions to the share and not to the directory, didn't you? and there are no group policies specifying against Anon User?

have you tried changing the credentials on your local box under which IIS runs (i.e. change anon login to your AD user name (using browse so as to be sure to pick up the domain properly) and password [don't let IIS manage the pwd])?

and you're not using php safe mode are you?

i don't understand the invalid argument in fopen. could you try explicitly hard coding the url/unc and adding "rb". the binary flag should not matter for a simple open but it's worth doing as a matter of course.

Code:
fopen("//server/share/filenmame.ext", "rb")
 
So uh, what you said should take care of it... but in case it's still not... you need to run a whois, make sure your script is executing as who you think it is.

And I'm a bit uneducated in the IIS world, but are you working with CGI then? Does IIS have all sorts of places to set security that you need to figure out? I would actually guess you should take this question over to the IIS forum and get yourself an answer in a post or two.
 
nope: IIS security is pretty straightforward. there is a snap-in that handles it.

the OP's issue (is/may be) with NT security and again that is set in one place via a properties menu. However a local drive can be "shared" multiple times with different names and each share, as well as the underlying local directory, can have different ACL's. Another thing: it is always better to set permissions locally i.e. you should not assume that just because you are an admin rights and can see a remote share, that changing the permissions on that remote share from a remote location will have the desired effect. Termserv into the box and change them locally.

i'm not sure that there is a "whois" equivalent for the NT world. I guess some bright spark has written one but i have not come across it.
 
I actually meant whoami not whois... and as with most of the useful *nix tools you can find a windows version at gnuwin32.sourceforge.net
 
J,


I added the ANON permissions to the share, to the directory, both and neither :) All roads lead to false. I change permissions from the original box as a practice...

Nothing fancy going on in Group Policy...

I tried the explicit code declaration, using "r" and "rb", and same error.

I'm hesitant to mess with permissions as this script is on the production intranet box. I'm setting up a VMWare box with PHP today to muck around with that.

How do I determine what user context PHP is running under?
 
have a look at the security panel in the iis manager snapin for the directory in which the script resides (or is called from).

i don't know what more to say though. it works for me across an active directory (native 2003) domain but when i screwed the permissions (deliberately) i got the same behaviour as you (apart from the illegal argument thing). so i'm pretty convinced it's a permissions issue rather than a php problem. as skyflyer says, you might bet better help from the windows or IIS fora.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top