471,071 Members | 1,205 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

file_exists works inconsistently?

I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need
to detect if a file exists, and if so, unlink it.
I am mystified as to why the file_exists() function does not work in
one particular section of code, yet does work in another section. The
unlink() function similarly works in one section, but not the other.

I have read other posts in this group where others have had trouble
with the file_exists() function, but none seemed to help with my
problem; so here goes...

Here is the code snippet that does not work, which includes a test to
verify if 'safe mode' is off:

if ($debug) {
echo "<br>fullname of target file = ".$fullname;
echo "<br>File exists? ";
if (file_exists($fullname)) {
echo "YES!";
} else {
echo "NO!!";
}
echo "<br>Safe mode on? ";
if (ini_get('safe_mode')) {
echo "YES!";
} else {
echo "NO!!";
}
echo "<br>(exiting...)";
exit;

Here is the (partial) output:

fullname of target file = /Net/delta/home/ehringer/MAELab/RickLIMStest/
GABA_JSakai/GABRGI/rs10033451/caller_1_results/
GABRGI_rs10033451_080307_VS_cda01_cda02_cda03_cda0 4.txt
File exists? NO!!
Safe mode on? NO!!
(exiting...)

By cut-&-paste into a console window with ls -la command, I verify the
full pathname is correct.

If anyone has any ideas about this, I would be most grateful...

Thanks,
Rick Casey

Aug 5 '08 #1
3 1941
On Aug 5, 8:31*pm, rickcasey <caseyr...@gmail.comwrote:
I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need
to detect if a file exists, and if so, unlink it.
I am mystified as to why the file_exists() function does not work in
one particular section of code, yet does work in another section. The
unlink() function similarly works in one section, but not the other.

I have read other posts in this group where others have had trouble
with the file_exists() function, but none seemed to help with my
problem; so here goes...

Here is the code snippet that does not work, which includes a test to
verify if 'safe mode' is off:

* * * * if ($debug) {
* * * * * * * * echo "<br>fullname of target file = ".$fullname;
* * * * * * * * echo "<br>File exists? ";
* * * * * * * * if (file_exists($fullname)) {
* * * * * * * * * * * * echo "YES!";
* * * * * * * * } else {
* * * * * * * * * * * * echo "NO!!";
* * * * * * * * }
* * * * * * * * echo "<br>Safe mode on? ";
* * * * * * * * if (ini_get('safe_mode')) {
* * * * * * * * * * * * echo "YES!";
* * * * * * * * } else {
* * * * * * * * * * * * echo "NO!!";
* * * * * * * * }
* * * * * * * * echo "<br>(exiting...)";
* * * * * * * * exit;

Here is the (partial) output:

fullname of target file = /Net/delta/home/ehringer/MAELab/RickLIMStest/
GABA_JSakai/GABRGI/rs10033451/caller_1_results/
GABRGI_rs10033451_080307_VS_cda01_cda02_cda03_cda0 4.txt
File exists? NO!!
Safe mode on? NO!!
(exiting...)

By cut-&-paste into a console window with ls -la command, I verify the
full pathname is correct.

If anyone has any ideas about this, I would be most grateful...

Thanks,
Rick Casey
Try the new PHP4 version there are many bugfixes in file_exists.
Do the same test but with is_file(). Did you find the file with the
glob?

Alexander
Aug 6 '08 #2
rickcasey wrote:
I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need
to detect if a file exists, and if so, unlink it.
I am mystified as to why the file_exists() function does not work in
one particular section of code, yet does work in another section. The
unlink() function similarly works in one section, but not the other.

I have read other posts in this group where others have had trouble
with the file_exists() function, but none seemed to help with my
problem; so here goes...

Here is the code snippet that does not work, which includes a test to
verify if 'safe mode' is off:

if ($debug) {
echo "<br>fullname of target file = ".$fullname;
echo "<br>File exists? ";
if (file_exists($fullname)) {
echo "YES!";
} else {
echo "NO!!";
}
echo "<br>Safe mode on? ";
if (ini_get('safe_mode')) {
echo "YES!";
} else {
echo "NO!!";
}
echo "<br>(exiting...)";
exit;

Here is the (partial) output:

fullname of target file = /Net/delta/home/ehringer/MAELab/RickLIMStest/
GABA_JSakai/GABRGI/rs10033451/caller_1_results/
GABRGI_rs10033451_080307_VS_cda01_cda02_cda03_cda0 4.txt
File exists? NO!!
Safe mode on? NO!!
(exiting...)

By cut-&-paste into a console window with ls -la command, I verify the
full pathname is correct.

If anyone has any ideas about this, I would be most grateful...
1.) add a trim:
if (file_exists($fullname)) {
< if (file_exists(trim($fullname))) {
to make sure there are no whitespaces in $fullname, so that you actually
test for two different paths.
2.) any fancy encodings around ? Probably not, but if so that might affect
interpretation of paths
3.) what about the permissions of containing folders ? everything readable
and the web server is allowed to change into _all_ containing folders ?
(guessing that you run the script under a web servers process...)

arkascha

Aug 6 '08 #3
On Aug 5, 7:31 pm, rickcasey <caseyr...@gmail.comwrote:
I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need
to detect if a file exists, and if so, unlink it.
I am mystified as to why the file_exists() function does not work in
one particular section of code, yet does work in another section. The
unlink() function similarly works in one section, but not the other.

I have read other posts in this group where others have had trouble
with the file_exists() function, but none seemed to help with my
problem; so here goes...
<snip>

So you run file_exists on the same file twice in your code without
calling clearstatcache() - I suspect you should RTFM for both
functions.

C.
Aug 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Chann Becker | last post: by
10 posts views Thread by Google Mike | last post: by
1 post views Thread by Hinrich Specht | last post: by
3 posts views Thread by annie | last post: by
5 posts views Thread by lkrubner | last post: by
4 posts views Thread by dchaffin | last post: by
20 posts views Thread by Bob Sanderson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.