Page 1 of 1

file_exists function (CONCLUDED)

Posted: Thu Jun 21, 2007 2:17 am
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

Posted: Thu Jun 21, 2007 2:32 am
by Gente
Try do apply absolute path. You can use $_SERVER['DOCUMENT_ROOT'] for this.

Posted: Thu Jun 21, 2007 2:47 am
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.

Posted: Thu Jun 21, 2007 3:02 am
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.

Posted: Thu Jun 21, 2007 4:43 am
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
-

Posted: Thu Jun 21, 2007 6:23 am
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).

Posted: Thu Jun 21, 2007 6:30 am
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
-

Posted: Thu Jun 21, 2007 7:32 am
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.)

Posted: Thu Jun 21, 2007 11:07 am
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).

Posted: Thu Jun 21, 2007 5:28 pm
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.

Posted: Thu Jun 21, 2007 7:45 pm
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.

Conclusion but needs to confirm from the group

Posted: Thu Jun 21, 2007 8:16 pm
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";
}