file check

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
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

file check

Post 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";
}
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
magosla2001
Forum Newbie
Posts: 20
Joined: Sat May 20, 2006 8:54 am

Post 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) )
Post Reply