php/jquery - check if image in url really exists
Get the solution ↓↓↓Solution:
You will probably want to use HEAD requests when getting headers (it doesn't do that by default):
stream_context_set_default(array(
'http' => array(
'method' => 'HEAD'
)
));
Second, you can pass a second parameter toget_headers
to parse it for you:
$headers = get_headers($imageurl, 1);
Then, you can check the rest as per normal:
if (strpos($headers[0], '200') === false) {
echo "not valid";
exit;
}
if (isset($headers['Content-Type']) && 0 === strncmp($headers['Content-Type'], 'image/', 6)) {
echo "valid image";
} else {
echo "probably not an image";
}
Answer
Solution:
also with get_headers ... one of them will be
image/png
image/jpeg
image/gif
so
$isImage = false;
foreach($headers as $header){
if(strpos($header,'image/')==true){
$isImage = true;
}
}
Answer
Solution:
$headers = @get_headers($imageurl);
$is_image = false;
foreach ($headers as $header) {
if (strpos($header, 'Content-Type: image/') === 0) {
$is_image = true;
break;
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.