What is the best way to verify if a user-inputted URL points to an image file using PHP?
To verify if a user-inputted URL points to an image file using PHP, you can use the getimagesize() function. This function returns an array with image information if the URL points to an image file, or false if it does not. You can then check if the returned value is an array to determine if the URL is an image file.
$url = "https://example.com/image.jpg";
$image_info = getimagesize($url);
if ($image_info !== false) {
echo "The URL points to an image file.";
} else {
echo "The URL does not point to an image file.";
}
Keywords
Related Questions
- What potential issues can arise when using array_unshift in PHP?
- How can foreach loops be utilized in PHP to process an unknown number of data records from a form submission?
- What are some considerations for structuring CSV files in a way that facilitates easier parsing and manipulation in PHP, especially when dealing with multi-line entries?