Jump to content

fopen returns true but there is no file there???


Allenph9

Recommended Posts

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?!

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.

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). 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.