pkirsch Posted May 2, 2007 Share Posted May 2, 2007 Hi, I have a few questions regarding File Handling! If you use fopen to open a file, and the file doesn't exist, it will create it. Right? What happens if you never close a file using fclose and just leave it open? Quote Link to comment https://forums.phpfreaks.com/topic/49692-solved-file-handling-questions/ Share on other sites More sharing options...
The Little Guy Posted May 2, 2007 Share Posted May 2, 2007 No. you need to check if the file exists before you actually open it, then you won't get errors about a non existing file. if you use fwrite, then the file will be created if it doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/49692-solved-file-handling-questions/#findComment-243652 Share on other sites More sharing options...
Barand Posted May 2, 2007 Share Posted May 2, 2007 If you open a file in write mode <?php $fp=fopen('empty.txt', 'w'); fclose($fp); // creates new empty file (no fwrite required) ?> it does not need to exist. If it doesn't exist the new file is create, if it does exist then the existing file is overwritten. Quote Link to comment https://forums.phpfreaks.com/topic/49692-solved-file-handling-questions/#findComment-243657 Share on other sites More sharing options...
The Little Guy Posted May 2, 2007 Share Posted May 2, 2007 Oops, sorry I should have looked at the manual first. You should do a check first though: <?php $filename = '/path/to/foo.txt'; $fp=fopen($filename, 'w'); if(file_exists($filename)){ fclose($fp); // creates new empty file (no fwrite required) }else{ echo 'Flie Doesn\'t Exist.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49692-solved-file-handling-questions/#findComment-243663 Share on other sites More sharing options...
pkirsch Posted May 2, 2007 Author Share Posted May 2, 2007 Thanks so much guys! Have a great day, pkirsch Quote Link to comment https://forums.phpfreaks.com/topic/49692-solved-file-handling-questions/#findComment-243744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.