fopen issue [SOLVED]

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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

fopen issue [SOLVED]

Post by Jade »

Hey there,

I was wondering if anyone knows how to workaround an fopen issue I've been having. Users on my website can link to photos, and if the photo isn't there then of course I get an fopen message: fopen(http://www.spendthriftfarm.com/Photos/m ... -brown.jpg): failed to open stream: HTTP request failed! HTTP/1.1 404

that everyone sees. Is there any good way to check if the photo is there before I call fopen so I don't have this issue? I've tried things like this but nothing seems to be working

Code: Select all

$fp = fopen($file, 'r');

if ($fp)
 ;//display photo
else
 ; //photo not found
Last edited by Jade on Mon Dec 26, 2005 11:33 am, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

if(file_exists('file')) {

// open it
} else {
echo 'technical difficulties';
}
you should also log errors instead of displaying them, avoid this issue all together

Another way to temporarily suppresss errors is with @

Code: Select all

@fopen('file');
will never show an error
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

file_exists($file) never seems to work for me, but supressing the warning did. Thanks.

Jade
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Post code of where file_exists() didn't work.. suppressing the error isn't "wrong" per 'se but you'll be better off doing it the "right" way


also try calling
clearstatcache () right before the call to file_exists()
Post Reply