file_exists function (CONCLUDED)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

file_exists function (CONCLUDED)

Post by rozvinbm_jp »

I can access http://localhost/websites/personal_site/others/test.ini via internet explorer.

File Structure: resides at http://localhost/websites/
Image

Code: Select all

<?PHP
//default.php
$filename = '/others/test.ini';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exists";
}
?>
result: The file /others/test.ini does not exists.

is there any configuration or wrong string value of $filename

settings:
Zend Studio v5.0 Enterprise Ed.
PHP5
Apache2.2.4
Last edited by rozvinbm_jp on Fri Jun 22, 2007 6:10 am, edited 1 time in total.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Try do apply absolute path. You can use $_SERVER['DOCUMENT_ROOT'] for this.
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post by rozvinbm_jp »

Code: Select all

//default.php - revised ver2
$filename = $_SERVER['DOCUMENT_ROOT'] . '/websites/personal_site/others/test.ini';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
Yes I tried that. it works.. that's why I am asking what's wrong with the previous code using $filename = '/others/test.ini';. Something strange, isn't?

Code: Select all

//default.php - revised ver3
$filename = 'default.php';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
How about this.. It returns also false in the condition if (file_exists($filename)) {.

Please share your experiences about this guys.
Thanks a lot. a r i g a t o u g o z a i m a s u.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Try to follow each single folder. This function also allows to check if folder exists.
Also you can put some script in `others` folder. Make an error there and copy the path of this folder.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-

Code: Select all

/others/test.ini
This path points to the root of your webservers html folder.

http://localhost/websites/personal_site/others/test.ini

So I guess it will look for a folder called "others" in here: http://localhost/others/

If you try to do the file_exists with folder "websites" or "personal_site" it will work.


To make your script work, try using relative paths:

Code: Select all

$filename = './others/test.ini';
djot
-
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

file_exists() operates on the filesystem of the server, not the web root. /other means you're looking for a folder named other right at the root of the server, not the web root (i.e. the C:\ drive in your case).
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
file_exists() operates on the filesystem of the server, not the web root.
Are you sure with that?
I guess the root is the public_html folder, and you can't go more left than the webroot with PHP.

djot
-
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

djot wrote:Are you sure with that?
I guess the root is the public_html folder, and you can't go more left than the webroot with PHP.
public_html is the webroot. Root is often several further up (left.)
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

since present working directory for
default.php
and the directory others are same you can use like this:

$filename = 'others/test.ini';

Code: Select all

if (file_exists($filename)) { 
    echo "The file $filename exists"; 
} else { 
    echo "The file $filename does not exist"; 
}
When the php interprets the default.php, it looks for the path: others/test.ini
in the directory where the script (that is being interpreted by it).
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

Try out this:

Code: Select all

$filename = dirname(__FILE__) . '\others\test.ini';
I used back slashes because dirname() on Windows does also produce back slashes.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

WaldoMonster wrote:Try out this:

Code: Select all

$filename = dirname(__FILE__) . '\others\test.ini';
I used back slashes because dirname() on Windows does also produce back slashes.
PHP works with either slash for me when I do that.
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Conclusion but needs to confirm from the group

Post by rozvinbm_jp »

file_exists() is checking starts from the root ( C:\ , /, \, C: ) not the webroot . I think the whole FileSystem in PHP must be the same behavior (any can prove this conclusion of FileSystem?).

Here's the simulation.

Directory Structure:
Image

*NOTE: All these statements returns true during if() condition evaluation.

Code: Select all

// 'BIOS' only returns false. Therefore, 'BIOS/7e2_0101.bin' also returns false
$filename = '/BIOS'; // folder located at C:\
if (file_exists($filename)) {
    echo "The $filename exists";
} else {
    echo "The $filename does not exist";
}

$filename = '/BIOS/7e2_0101.bin'; // Even 'C:\BIOS\7e2_0101.bin' returns true.
if (file_exists($filename)) {
    echo "The $filename exists";
} else {
    echo "The $filename does not exist";
}

// 'FileLocatedInTheROOT.txt' returns false
// '\FileLocatedInTheROOT.txt' returns false
$filename = '/FileLocatedInTheROOT.txt'; // text file located at C:\
if (file_exists($filename)) {
    echo "The $filename exists";
} else {
    echo "The $filename does not exist";
}

$filename = 'C:/COMPAQ/3Mode_Floppy'; // Even '/COMPAQ/3Mode_Floppy' returns true.
if (file_exists($filename)) {
    echo "The $filename exists";
} else {
    echo "The $filename does not exist";
}


$filename = '\\'; // C:\ is '\' in this case. Even '/' or 'C:' or 'C:\' are both returns true.
if (file_exists($filename)) {
    echo "The $filename exists";
} else {
    echo "The $filename does not exist";
}
Post Reply