galvin Posted October 19, 2011 Share Posted October 19, 2011 I hope this is enough info, but I have this code (at the bottom) that grabs images from my S3 account (Amazon Web Services)... If there is no bucket for the name given, it gives this warning... Warning: S3::getBucket(): [NoSuchBucket] The specified bucket does not exist in /home/xxxxxxx/public_html/xxxxxxx.com/S3.php on line 126 Assuming it's not specific syntax to S3 and is general PHP syntax, how could I prevent that warning from showing, by saying in plain English "If a bucket by that name doesn't exist, I'm gonna do something else and NOT spit out this warning?" Basically, I want to echo a message like "There is no bucket yet" instead of having that ugly warning. // Get the contents of our bucket $contents = $s3->getBucket("mybucket"); foreach ($contents as $file){ $fname = $file['name']; $furl = "http://mybucket.s3.amazonaws.com/".$fname; //output a link to the file echo "<a href=\"$furl\">$fname</a><br />"; echo "<img src='$furl' width='50' /><br />"; echo "$furl<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/249355-preventing-a-warning-message-from-showing/ Share on other sites More sharing options...
trq Posted October 19, 2011 Share Posted October 19, 2011 Generally functions or methods should return either true or false depending on wether or not they fail or succeed. This makes them easy to handle using logic like: if ($contents = $s3->getBucket("mybucket")) { foreach ($contents as $file) { // rst of code } } Clearly, whatever $s3 is pretty poorly written as it appears to be triggering a warning. I would investigate the getBucket() method itself and have it either return true or false, or if really necessary an exception. At least they are easy to handle. Quote Link to comment https://forums.phpfreaks.com/topic/249355-preventing-a-warning-message-from-showing/#findComment-1280470 Share on other sites More sharing options...
galvin Posted October 19, 2011 Author Share Posted October 19, 2011 Ok will do, thanks Thorpe! Quote Link to comment https://forums.phpfreaks.com/topic/249355-preventing-a-warning-message-from-showing/#findComment-1280481 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.