Allenph9 Posted March 5, 2012 Share Posted March 5, 2012 So in my last thread I was working on checking if a file was there...I resorted to something not in the library and used $pic_exists_jpg = fopen($jpg_path,'r'); $pic_exists_jpeg = fopen($jpeg_path,'r'); $pic_exists_gif = fopen($gif_path,'r'); $pic_exists_png = fopen($png_path,'r'); if ($pic_exists_jpg != '0') { $jpg_test = '1'; } elseif ($pic_exists_jpg == '1') { $jpg_test = '2'; } elseif ($pic_exists_jpg == '0') { $jpg_test = '2'; } elseif ($pic_exists_jpeg != '0') { $jpeg_test = '1'; } elseif ($pic_exists_gif != '0') { $gif_test = '1'; } elseif ($pic_exists_png != '0') { $png_test = '1'; } now for some reason php is being a poo and no matter what returning that there is a file there. no matter what method i use be it fopen or file_exists or anything else, it saying I have a file in that directory BUT I DO NOT! THERE IS NOT ONE OF THOSE FILES. I know I have my paths right because If I echo them directly I get the picture...what the heck?! Link to comment https://forums.phpfreaks.com/topic/258285-fopen-returns-true-but-there-is-no-file-there/ Share on other sites More sharing options...
Pikachu2000 Posted March 5, 2012 Share Posted March 5, 2012 When posting code, enclose it within the forum's . . . BBCode tags. Link to comment https://forums.phpfreaks.com/topic/258285-fopen-returns-true-but-there-is-no-file-there/#findComment-1324020 Share on other sites More sharing options...
AyKay47 Posted March 5, 2012 Share Posted March 5, 2012 if you are simply wanting to check for a files existence, you should be using file_exists. Rewrite the code using that function and provide a little more of the relevant code so we can more accurately attack this issue. Link to comment https://forums.phpfreaks.com/topic/258285-fopen-returns-true-but-there-is-no-file-there/#findComment-1324097 Share on other sites More sharing options...
ManiacDan Posted March 5, 2012 Share Posted March 5, 2012 fopen does not return true, ever. It returns either a file handler resource or false. According to my chart here, objects of type resource will never be equal to an integer (which is what you're using instead of true/false). Link to comment https://forums.phpfreaks.com/topic/258285-fopen-returns-true-but-there-is-no-file-there/#findComment-1324135 Share on other sites More sharing options...
AyKay47 Posted March 5, 2012 Share Posted March 5, 2012 fopen does not return true, ever. It returns either a file handler resource or false. According to my chart here, objects of type resource will never be equal to an integer (which is what you're using instead of true/false). I almost have that chart memorized now Dan..heh But Dan is absolutely right, and I overlooked it in my OP. When using a function that returns a resource or false upon failure, I would use negation to check the return value against the boolean FALSE return. if(fopen("file.txt","r") !== false) or check for it returning false in an if else statement. if(fopen("file.txt","r") === false) { //returned false } else { //returned resource } Anyway, using file_exists is what I would go with. Bookmark Dan's chart, it will come in handy in the future. Link to comment https://forums.phpfreaks.com/topic/258285-fopen-returns-true-but-there-is-no-file-there/#findComment-1324145 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.