By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Anyone Have A Better file_exists() ?

P: n/a
{NOTE: I have PHP 4.2.2 for RH9 Linux.}

Anyone have a better file_exists() out there? Even if you use shell
out tricks with Linux using the `command` trick, I'd be interested to
see what you came up with.

I have this remotely mounted SMB shares on Linux, stuck in my /mnt
directory, and the docs tell me that file_exists() doesn't work on
that. When I tested this, I found this to be true. Therefore, I need
an alternative.
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 27 Jul 2004 13:25:24 -0700
go********@hotpop.com (Google Mike) wrote:
{NOTE: I have PHP 4.2.2 for RH9 Linux.}

Anyone have a better file_exists() out there? Even if you use shell
out tricks with Linux using the `command` trick, I'd be interested to
see what you came up with.

I have this remotely mounted SMB shares on Linux, stuck in my /mnt
directory, and the docs tell me that file_exists() doesn't work on
that. When I tested this, I found this to be true. Therefore, I need
an alternative.


It seems that:
test -e <file>; echo $? works fine...

I guess you could do something like:

<?php

// Array of files to check.
$file = array(
$_ENV['HOME'], // Should exist.
$_ENV['HOME'] . "/jaiewrpfjaoiejf" // Should not exist.
);

// Command to run.
$cmd = 'test -e';

// "Just do it(r)"
foreach ($file as $key => $val) {
exec("$cmd $val", $dummy_array, $return);
if ($return == 0) {
echo "$key: $val exist!\n";
} else {
echo "$key: $val doesn't exist!\n";
}
}
?>

NOTE: Remember that shell-programs return 0 on success and not-0 on
failure, thus the if ($return == 0) { // success }

'test -e' checks for anything in the filesystem (dirs, files, pipes,
symlinks --- the whole lot), if you just want to check for files use
'test -f' or see 'man test' for more info.

Best regards
Madsen

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBBuSllNHJe/JASHcRAo22AJ9nL2erMBnhJbuVffQYf0U9MGqDBgCfeb9L
XQ9ZOlqT9jva+qsMplm3LfE=
=Bcgl
-----END PGP SIGNATURE-----

Jul 17 '05 #2

P: n/a
"Google Mike" wrote:
{NOTE: I have PHP 4.2.2 for RH9 Linux.}

Anyone have a better file_exists() out there? Even if you use shell
out tricks with Linux using the `command` trick, I’d be
interested to
see what you came up with.

I have this remotely mounted SMB shares on Linux, stuck in my /mnt
directory, and the docs tell me that file_exists() doesn’t work
on
that. When I tested this, I found this to be true. Therefore, I need an alternative.


Why would it not work, and where in the doc it says it does not work.
I cannot find that in: http://ca2.php.net/file_exists

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-file_exi...ict133885.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=447124
Jul 17 '05 #3

P: n/a
Google Mike wrote:
{NOTE: I have PHP 4.2.2 for RH9 Linux.}

Anyone have a better file_exists() out there? Even if you use shell
out tricks with Linux using the `command` trick, I'd be interested to
see what you came up with.

I have this remotely mounted SMB shares on Linux, stuck in my /mnt
directory, and the docs tell me that file_exists() doesn't work on
that. When I tested this, I found this to be true. Therefore, I need
an alternative.


What if you just try opening the file for reading ? Would that not yield a
failure if the file is not present ?
HTH
Pjotr
Jul 17 '05 #4

P: n/a
On Wed, 28 Jul 2004 15:15:15 +0200
"Pjotr Wedersteers" <x3****@westerterp.com> wrote:
What if you just try opening the file for reading ? Would that not
yield a failure if the file is not present ?


Yeah, it would, but it'd also be slower than necessary.

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBB7D8lNHJe/JASHcRAm+mAJ92DZghE3JNGzlhYTaGnPmpvpe27wCcCQ4z
kRSprvWN9WZoFo6a3Hvp5PU=
=6PQI
-----END PGP SIGNATURE-----

Jul 17 '05 #5

P: n/a
Anders K. Madsen wrote:
On Wed, 28 Jul 2004 15:15:15 +0200
"Pjotr Wedersteers" <x3****@westerterp.com> wrote: What if you just try opening the file for reading ? Would that not
yield a failure if the file is not present ?
Yeah, it would, but it'd also be slower than necessary.


--

Well, I just put it to the test. You'd be surprised perhaps (I wasn't).

Using your 'test -e' shell command execution testing for a files presence
took just over 1.1 seconds on my P3-500 LAMP box.
Using 'my' @fopen ($file,'r'); also a hundred times took 0.021 seconds. (@to
suppress warnings)

Why did you think executing a shell command would be faster ?
Pjotr
Jul 17 '05 #6

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 28 Jul 2004 18:01:19 +0200
"Pjotr Wedersteers" <x3****@westerterp.com> wrote:
Anders K. Madsen wrote:
On Wed, 28 Jul 2004 15:15:15 +0200
"Pjotr Wedersteers" <x3****@westerterp.com> wrote: What if you just try opening the file for reading ? Would that not
yield a failure if the file is not present ?
Yeah, it would, but it'd also be slower than necessary.


Please note that I didn't say "compared to the shell-way".

Well, I just put it to the test. You'd be surprised perhaps (I
wasn't).

Using your 'test -e' shell command execution testing for a files
presence took just over 1.1 seconds on my P3-500 LAMP box.
Using 'my' @fopen ($file,'r'); also a hundred times took 0.021
seconds. (@to suppress warnings)

Why did you think executing a shell command would be faster ?


I didn't actually... What I thought would be faster was file_exists().

I'm not the least bit surprised that doing it through a shell is slower
than opening an closing the file, since you'd have PHP invoking a shell
to do it. (Might actually be faster if you do it through proc_open();)

But my reasoning for thinking that file_exists() is faster than doing it
with fopen() is that file_exists() doesn't have to open and close a
file.

Furthermore, it might also have impact whether or not the file actually
exists. (I'll try testing it myself.)

Best regards,
Madsen

- --
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBB+YXlNHJe/JASHcRAhWGAJ9gd9xDHV6VoLAUjEzOzfOwTHUGtQCeOZ6U
kE1A8f/1RQMac5jUW+JcN+I=
=x0aO
-----END PGP SIGNATURE-----
Jul 17 '05 #7

P: n/a
"Anders K. Madsen" <ma****@lillesvin.linux.dk> wrote in message
go********@hotpop.com (Google Mike) wrote:
{NOTE: I have PHP 4.2.2 for RH9 Linux.}

Anyone have a better file_exists() out there? Even if you use shell
out tricks with Linux using the `command` trick, I'd be interested to
see what you came up with.

I have this remotely mounted SMB shares on Linux, stuck in my /mnt
directory, and the docs tell me that file_exists() doesn't work on
that. When I tested this, I found this to be true. Therefore, I need
an alternative.


It seems that:
test -e <file>; echo $? works fine...

I guess you could do something like:

<?php

// Array of files to check.
$file = array(
$_ENV['HOME'], // Should exist.
$_ENV['HOME'] . "/jaiewrpfjaoiejf" // Should not exist.
);

// Command to run.
$cmd = 'test -e';

// "Just do it(r)"
foreach ($file as $key => $val) {
exec("$cmd $val", $dummy_array, $return);
if ($return == 0) {
echo "$key: $val exist!\n";
} else {
echo "$key: $val doesn't exist!\n";
}
}
?>

NOTE: Remember that shell-programs return 0 on success and not-0 on
failure, thus the if ($return == 0) { // success }

'test -e' checks for anything in the filesystem (dirs, files, pipes,
symlinks --- the whole lot), if you just want to check for files use
'test -f' or see 'man test' for more info.

Best regards
Madsen


I adapted your solution here and it works on Linux. Thank you very
much.

function exists_file($file) {
return (`test -e "$file";echo $?`==0);
}

(For newbies who read this, note that I used the backtick (`)
operator.)

Also, you should experiment and see what happens when you take off the
==0 and the () wrap. You'll be disappointed. You'll find that you have
to use these to get the result you want.
Jul 17 '05 #8

P: n/a
Google Mike wrote:
{NOTE: I have PHP 4.2.2 for RH9 Linux.}

Anyone have a better file_exists() out there? Even if you use shell
out tricks with Linux using the `command` trick, I'd be interested to
see what you came up with.

I have this remotely mounted SMB shares on Linux, stuck in my /mnt
directory, and the docs tell me that file_exists() doesn't work on
that. When I tested this, I found this to be true. Therefore, I need
an alternative.


I use file_exists() on remote mounted files and it works.

Set-up:
Linux RH6.2, Samba 2.2.7

My remote mounted SMB shares are NOT mounted in /mnt.

The SMB shares are directories on a Windows XP Pro box.

Jul 17 '05 #9

P: n/a
2metre wrote:
Set-up:
Linux RH6.2, Samba 2.2.7


Correction: that was supposd to read RH7.2. PHP=v4.3.4

Test script was running as PHP as CGI.

My entire Web root is mounted from the Windows box because my preferred
version control is Microsoft Visual Source Safe.
Jul 17 '05 #10

P: n/a
"Pjotr Wedersteers" <x3****@westerterp.com> wrote in message
Well, I just put it to the test. You'd be surprised perhaps (I wasn't).

Using your 'test -e' shell command execution testing for a files presence
took just over 1.1 seconds on my P3-500 LAMP box.
Using 'my' @fopen ($file,'r'); also a hundred times took 0.021 seconds. (@to
suppress warnings)

Why did you think executing a shell command would be faster ?
Pjotr


What if the file is 40MB? Does fopen operate more slowly than 0.021
seconds then?

I'm willing to use the @fopen() technique if your results show me that
it's faster in such a case. For now, I'm using the exists_file()
routine that I adapted from Madsen and posted last night.
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.