Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How would you check if the result string of file_get_contents() is a JPG image?

Tags:

php

I am using file_get_contents() to pull some images from a remote server and I want to confirm if the result string is a JPG/PNG image before further processing, like saving it locally and create thumbs.

$string = file_get_contents($url);

How would you do this?

like image 944
Bertrand Avatar asked Mar 22 '13 14:03

Bertrand


People also ask

What will the file_get_contents () return?

Return Values ¶ The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is the difference between file_get_contents () function and file () function?

They both read an entire file, but file reads the file into an array, while file_get_contents reads it into a string.

What is the difference between file_get_contents ($ file and file_get_contents ($ file in PHP?

$file: It specifies the file in which you want to write. $data: It specifies the data that has to be written on the file. It can be a string, an array, or a data stream.


1 Answers

You can use

exif_imagetype()

to evaluate your file. Please do check the php manual.

Edited :

Please note that PHP_EXIF must be enabled. You can read more about it here

like image 153
Moch. Rasyid Avatar answered Oct 06 '22 23:10

Moch. Rasyid