validate if correct image url php

Solutions on MaxInterview for validate if correct image url php by the best coders in the world

showing results for - "validate if correct image url php"
Valeria
30 May 2019
1$image_name = $_FILES["inputname"]["name"];
2$allowed_extensions = array("png", "jpg", "jpeg");
3$image_extension = explode(".", $image_name);
4/*
5  *Explode returns array of words
6  *Explode Example:
7  	explode(search_for, where to search for);
8    let imagine that the image named => myimage.png
9    expldoe funtion will search the word for '.' and explode it
10    from the example, explode will return array("myimage", "png")
11    Now we find our image extension, but it is in the array.
12    we use end() function that will return the last index in the array as a string
13*/
14$extension = end($image_extension); // which is png
15if(!in_array($extension, $allowed_extensions)){
16  echo "Please upload an image";
17}
18else{
19  echo "Allowed Image";
20}
21
Clara
09 Aug 2017
1If you want to be absolutely sure, and your PHP is enabled for remote 
2connections, you can just use "getimagesize('url')"
3
4function validImage($url) {
5   return stripos(getimagesize($url)['mime'], 'image') === 0;
6}
7
8$image = validImage('http://www.example.com/image.jpg');
9
10If it returns an array, it is an image type recognized by PHP, even if the 
11image extension is not in the url (per your second link). You have to keep in
12mind that this method will make a remote connection for each request, so 
13perhaps cache urls that you already probed in a database to lower connections.