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!
<?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.
//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?
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.
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).
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:
*NOTE: All these statements returns true during if() condition evaluation.
// '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";
}