Code:
if(isset($_POST['update_avatar'])) {
$url = $_POST['avatar'];
$info = getimagesize($url);
if(isset($info['name'])) {
echo "Exists";
} else {
echo "Error";
}
}
How can I avoid getting PHP errors when the user types an invalid URL, random piece of text or invalid image URL etc?
If there's an error, getimagesize
returns false
, so test for that. And to suppress error messages from a function, put @
before it:
$info = @getimagesize($url);
if (!$info) {
echo "Error";
} else {
// Process the image
}
Use exception handling. Place your critical code into a try..catch block. You can find more information here.