473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

file_exists() function doesn't return a value

12
Hi all,

I ran into a cool function in a google search, called file_exists that would do exactly what I need it to do. Unfortunately, I think something is wrong with the logic I'm using, because it returns a value of null instead of true or false. It seems to work great if I check for something in the base directory, but when I try something outside of the root, it fails. The check file code looks like this:
Expand|Select|Wrap|Line Numbers
  1. if (file_exists($filename)) {
  2.     echo "The file $filename exists";
  3. } else {
  4.     echo "The file $filename does not exist";
  5. }
If I say $filename = 'abcdef.php' which exists in the root directory, it returns true and everything is fine. But, if $filename = '/dir1/dir2/abcdef.php', it doesn't return true or false.

Does anyone have any ideas if I am using the correct structure for specifying the directories?

Thanks!
Aug 19 '07 #1
15 9034
pbmods
5,821 Expert 4TB
Heya, cooldht.

Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

What OS is your server running?

Try checking for errors.
Aug 20 '07 #2
cooldht
12
Hi PB,

Sorry about leaving out the tags. I'm assuming you wanted to know the PHP version on the server. That is 4.3.8-1.1. Just in case, the MYSQL version is 3.23.58-4.

Thanks!

cooldht
Aug 20 '07 #3
cooldht
12
Sorry again,

I forgot to mention that I have the debugging on in the page, and didn't receive any error messages.

Thanks,

ct
Aug 20 '07 #4
pbmods
5,821 Expert 4TB
Heya, cooldht.

*n?x or Windows?
Aug 20 '07 #5
cooldht
12
I'm not the main person that deals with the server side of things, so forgive me if I don't answer correctly. I can access the server, so I can view certain statistics. I can see the OS running says Linux 2.6.8-022stab070.9-enterprise. Is that what you are looking for?

Thanks,

ct
Aug 20 '07 #6
pbmods
5,821 Expert 4TB
Heya, CT.

That'll do it. Thanks.

Can you verify that the file exists? For example:

Expand|Select|Wrap|Line Numbers
  1. var_dump(file_exists($filename));
  2. echo '<br />';
  3. fread($filename);
  4.  
If you see something like this:
Expand|Select|Wrap|Line Numbers
  1. NULL
  2. This is a sample file.
  3.  
Then you know that file_exists() is misbehaving because the file definitely exists.
Aug 20 '07 #7
cooldht
12
Hi PB,

I received the following when trying the code mentioned:

bool(false)

Warning: Wrong parameter count for fread() in /home/httpd/vhosts/httpdocs/pictures.php on line 59


The funny thing is, if I put the page in the same directory as where the file exists, it finds the file ok by making $filename equal to roses.gif only, instead of specifying /images/roses.gif.

Thanks,

ct
Aug 20 '07 #8
pbmods
5,821 Expert 4TB
Heya, CT.

One more:
Expand|Select|Wrap|Line Numbers
  1. var_dump($filename);
  2. echo '<br />';
  3.  
  4. var_dump(file_exists($filename));
  5. echo '<br />';
  6. fread($filename);
  7.  
The 'wrong parameter count' error generally means that the value of the first parameter to fread() is null, which would lead me to wonder if perhaps $filename is not getting set properly.
Aug 20 '07 #9
cooldht
12
Thanks for the reply.

Ok, this is what was returned this time around....

string(17) "/images/roses.gif"
bool(false)

Warning: Wrong parameter count for fread() in /home/httpd/vhosts/httpdocs/pictures.php on line 61

Any ideas?

Thanks!

CT
Aug 20 '07 #10
pbmods
5,821 Expert 4TB
Heya, CT.

My bad. I meant readfile().

At any rate, file_exists() is returning false because it's looking for a directory named 'images' at the root level of your server's hard drive.

Try this instead:
Expand|Select|Wrap|Line Numbers
  1. var_dump(file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $filename));
  2.  
Aug 20 '07 #11
jx2
228 100+
you can try "dot" for current dir or double dot for "parent directory
if your root contain two directories :

brother
sister

and your script is in sister
e.g. "./sister/sisterscript.php"

to acces brother you need:

e.g. "../brother/brotherscript.php"

regards
jx2
Aug 20 '07 #12
cooldht
12
Hi guys,

I selectively forgot to mention that I had tried the old ../ trick right off the bat, and didn't seem to do it. However, the DOCUMENT_ROOT trick seems to work ok. Thanks PB! I think I can make due doing it this way. Although, I'm a little surprised this worked, but the ../ trick didn't. Oh well, live and learn! Thanks for all the help!

CT
Aug 21 '07 #13
pbmods
5,821 Expert 4TB
Heya, CT.

Using '../' is a bit unpredictable because you don't necessarily know the current working directory.

Fortunately, PHP has a function for that!
Aug 21 '07 #14
cooldht
12
Thanks again PB. That could be very awesome. If that function works right for me, I could very well find myself doing some re-design on various websites as I think it could be a useful little gadget!

Thanks,

CT
Aug 21 '07 #15
pbmods
5,821 Expert 4TB
Heya, CT.

Good luck with your project, and if you ever need anything, post back anytime :)
Aug 21 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chann Becker | last post by:
My current setup: WWW Server: Windows 2000 Server, SP4 IIS5 PHP 4.3.4 File Server Windows 2000 Server, SP4
2
by: Andrew Crowe | last post by:
Hi guys, I've created this little function to check whether a user has uploaded a file with the same name as an existing file, and if so rename it to file-1.jpg, file-2.jpg etc. ...
10
by: Google Mike | last post by:
{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...
3
by: annie | last post by:
Hi I've got two linux boxes running redhat9 and either PHP Version 4.2.2 or PHP Version 4.3.7. The file structure is set up the same on both machines and both files exist. The problem is not...
5
by: lkrubner | last post by:
I've written some template code and one thing I'm trying to protect against is references to images that don't exist. Because users have the ability to muck around with the templates after...
6
by: +86 | last post by:
i encountered this problem: "include('inc.php')" will work problely but "include('./inc.php') doesn't work .. both file_exists('inc.php') or file_exists('./inc.php') didn't return the right...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
20
by: Bob Sanderson | last post by:
This is my code: if (file_exists($Fname)) { echo "<td>$Fname exists</td>"; } else { echo "<td>$Fname does not exist</td>"; } $Fname is the full path to the file I'm trying to verify. When I...
3
by: rickcasey | last post by:
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.