Page 1 of 1

file check

Posted: Sun Sep 24, 2006 5:46 am
by magosla2001
I wanted to check if a file exist in a directory and if it doent exist the flie should be created and if it does exist the user should be told to change the name of the file he or she intends to create in the directory.

I tried the below script but if the file doesn't exist, i receive this error;
Warning: file(ient.php) [function.file]: failed to open stream: No such file or directory in E:\Webserver\xampplite\htdocs\text\index.php on line 3
So I need help on this script so I dont receive the error.

Code: Select all

<?
$filename = $_POST["filename"].".txt";
if (file($filename) == null) {
echo "file not found";
}
else {
echo "file exists in the directory";
}
?>

Posted: Sun Sep 24, 2006 5:49 am
by Benjamin
Hmm, maybe you can glean something from the manual.. http://www.php.net/file

Your problem is that your testing to see if a variable is populated or not in the if statement, but the statement isn't checking to see if the file exists. Your solution is to first test to see of the file exists. You may want to search around the manual and see of there is a function that can test to see if a file exists or not. That would solve your problem.

Posted: Sun Sep 24, 2006 5:57 am
by magosla2001
I discovered i was suppose to use to check for the existence of the file with

Code: Select all

if ( !file_exists($filename) )