How to check file is valid image or not using php?
up vote
0
down vote
favorite
i need your suggestions for checking image file. If a user will upload any file with changed extension type like (jpg,jpeg,bmp,png) how do we figure out that in PHP?
php file file-type
|
show 2 more comments
up vote
0
down vote
favorite
i need your suggestions for checking image file. If a user will upload any file with changed extension type like (jpg,jpeg,bmp,png) how do we figure out that in PHP?
php file file-type
Why should that be of interest? If a user uploads something, then that is his own problem. Typically you only want to re-deliver that if it is requested. If someone uploaded a text file renamed somehow, then fine, he get's back a text file.
– arkascha
1 hour ago
Have you done anything on your own yet? You are expected to try to write the code yourself. Please read How to create a Minimal, Complete, and Verifiable example.
– kerbholz
1 hour ago
@arkascha that could open your application to security issues.
– Federico klez Culloca
1 hour ago
2
@FedericoklezCulloca Can you explain how that is possible?
– patrick
42 mins ago
@FedericoklezCulloca Only if you actually use such uploaded data and integrate it into your application logic. Which of course no one does specifically because you cannot in any way trust data uploaded from the client side. Yes, you can deliver a file that contains something else than what it appears to hold at first. So what? That is not a "security issue".
– arkascha
34 mins ago
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i need your suggestions for checking image file. If a user will upload any file with changed extension type like (jpg,jpeg,bmp,png) how do we figure out that in PHP?
php file file-type
i need your suggestions for checking image file. If a user will upload any file with changed extension type like (jpg,jpeg,bmp,png) how do we figure out that in PHP?
php file file-type
php file file-type
edited 1 hour ago
Federico klez Culloca
15.4k134274
15.4k134274
asked 1 hour ago
Vishnu Sharma
66
66
Why should that be of interest? If a user uploads something, then that is his own problem. Typically you only want to re-deliver that if it is requested. If someone uploaded a text file renamed somehow, then fine, he get's back a text file.
– arkascha
1 hour ago
Have you done anything on your own yet? You are expected to try to write the code yourself. Please read How to create a Minimal, Complete, and Verifiable example.
– kerbholz
1 hour ago
@arkascha that could open your application to security issues.
– Federico klez Culloca
1 hour ago
2
@FedericoklezCulloca Can you explain how that is possible?
– patrick
42 mins ago
@FedericoklezCulloca Only if you actually use such uploaded data and integrate it into your application logic. Which of course no one does specifically because you cannot in any way trust data uploaded from the client side. Yes, you can deliver a file that contains something else than what it appears to hold at first. So what? That is not a "security issue".
– arkascha
34 mins ago
|
show 2 more comments
Why should that be of interest? If a user uploads something, then that is his own problem. Typically you only want to re-deliver that if it is requested. If someone uploaded a text file renamed somehow, then fine, he get's back a text file.
– arkascha
1 hour ago
Have you done anything on your own yet? You are expected to try to write the code yourself. Please read How to create a Minimal, Complete, and Verifiable example.
– kerbholz
1 hour ago
@arkascha that could open your application to security issues.
– Federico klez Culloca
1 hour ago
2
@FedericoklezCulloca Can you explain how that is possible?
– patrick
42 mins ago
@FedericoklezCulloca Only if you actually use such uploaded data and integrate it into your application logic. Which of course no one does specifically because you cannot in any way trust data uploaded from the client side. Yes, you can deliver a file that contains something else than what it appears to hold at first. So what? That is not a "security issue".
– arkascha
34 mins ago
Why should that be of interest? If a user uploads something, then that is his own problem. Typically you only want to re-deliver that if it is requested. If someone uploaded a text file renamed somehow, then fine, he get's back a text file.
– arkascha
1 hour ago
Why should that be of interest? If a user uploads something, then that is his own problem. Typically you only want to re-deliver that if it is requested. If someone uploaded a text file renamed somehow, then fine, he get's back a text file.
– arkascha
1 hour ago
Have you done anything on your own yet? You are expected to try to write the code yourself. Please read How to create a Minimal, Complete, and Verifiable example.
– kerbholz
1 hour ago
Have you done anything on your own yet? You are expected to try to write the code yourself. Please read How to create a Minimal, Complete, and Verifiable example.
– kerbholz
1 hour ago
@arkascha that could open your application to security issues.
– Federico klez Culloca
1 hour ago
@arkascha that could open your application to security issues.
– Federico klez Culloca
1 hour ago
2
2
@FedericoklezCulloca Can you explain how that is possible?
– patrick
42 mins ago
@FedericoklezCulloca Can you explain how that is possible?
– patrick
42 mins ago
@FedericoklezCulloca Only if you actually use such uploaded data and integrate it into your application logic. Which of course no one does specifically because you cannot in any way trust data uploaded from the client side. Yes, you can deliver a file that contains something else than what it appears to hold at first. So what? That is not a "security issue".
– arkascha
34 mins ago
@FedericoklezCulloca Only if you actually use such uploaded data and integrate it into your application logic. Which of course no one does specifically because you cannot in any way trust data uploaded from the client side. Yes, you can deliver a file that contains something else than what it appears to hold at first. So what? That is not a "security issue".
– arkascha
34 mins ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
I will use mime_content_type is exists. Else execute linux command of file -i -b
on the file to get the answer.
Consider function as following:
function getFileType($file_name) {
if(! function_exists('mime_content_type')) {
$isUnix = strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && DIRECTORY_SEPARATOR === '/';
// check whether operating system is that of a UNIX type.
if ($isUnix) {
$type = null;
exec('file -i -b ' . realpath($file_name), $type);
$parts = @ explode(";", $type[0]); // can be of format text/plain; charset=us-ascii
return trim($parts[0]);
}
// the file program/command does not exist on Windows.
else {
return null;
}
} else {
return mime_content_type($file_name);
}
}
You can also use finfo-file is you prefer.
Or you can spare yourself the shellout and usefinfo_file
– Federico klez Culloca
1 hour ago
add a comment |
up vote
-3
down vote
You can check for the filetype with 'filetype()
' in PHP: PHP filetype
New contributor
3
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I will use mime_content_type is exists. Else execute linux command of file -i -b
on the file to get the answer.
Consider function as following:
function getFileType($file_name) {
if(! function_exists('mime_content_type')) {
$isUnix = strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && DIRECTORY_SEPARATOR === '/';
// check whether operating system is that of a UNIX type.
if ($isUnix) {
$type = null;
exec('file -i -b ' . realpath($file_name), $type);
$parts = @ explode(";", $type[0]); // can be of format text/plain; charset=us-ascii
return trim($parts[0]);
}
// the file program/command does not exist on Windows.
else {
return null;
}
} else {
return mime_content_type($file_name);
}
}
You can also use finfo-file is you prefer.
Or you can spare yourself the shellout and usefinfo_file
– Federico klez Culloca
1 hour ago
add a comment |
up vote
0
down vote
I will use mime_content_type is exists. Else execute linux command of file -i -b
on the file to get the answer.
Consider function as following:
function getFileType($file_name) {
if(! function_exists('mime_content_type')) {
$isUnix = strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && DIRECTORY_SEPARATOR === '/';
// check whether operating system is that of a UNIX type.
if ($isUnix) {
$type = null;
exec('file -i -b ' . realpath($file_name), $type);
$parts = @ explode(";", $type[0]); // can be of format text/plain; charset=us-ascii
return trim($parts[0]);
}
// the file program/command does not exist on Windows.
else {
return null;
}
} else {
return mime_content_type($file_name);
}
}
You can also use finfo-file is you prefer.
Or you can spare yourself the shellout and usefinfo_file
– Federico klez Culloca
1 hour ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I will use mime_content_type is exists. Else execute linux command of file -i -b
on the file to get the answer.
Consider function as following:
function getFileType($file_name) {
if(! function_exists('mime_content_type')) {
$isUnix = strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && DIRECTORY_SEPARATOR === '/';
// check whether operating system is that of a UNIX type.
if ($isUnix) {
$type = null;
exec('file -i -b ' . realpath($file_name), $type);
$parts = @ explode(";", $type[0]); // can be of format text/plain; charset=us-ascii
return trim($parts[0]);
}
// the file program/command does not exist on Windows.
else {
return null;
}
} else {
return mime_content_type($file_name);
}
}
You can also use finfo-file is you prefer.
I will use mime_content_type is exists. Else execute linux command of file -i -b
on the file to get the answer.
Consider function as following:
function getFileType($file_name) {
if(! function_exists('mime_content_type')) {
$isUnix = strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' && DIRECTORY_SEPARATOR === '/';
// check whether operating system is that of a UNIX type.
if ($isUnix) {
$type = null;
exec('file -i -b ' . realpath($file_name), $type);
$parts = @ explode(";", $type[0]); // can be of format text/plain; charset=us-ascii
return trim($parts[0]);
}
// the file program/command does not exist on Windows.
else {
return null;
}
} else {
return mime_content_type($file_name);
}
}
You can also use finfo-file is you prefer.
edited 29 secs ago
marvinIsSacul
27015
27015
answered 1 hour ago
David Winder
2,4361723
2,4361723
Or you can spare yourself the shellout and usefinfo_file
– Federico klez Culloca
1 hour ago
add a comment |
Or you can spare yourself the shellout and usefinfo_file
– Federico klez Culloca
1 hour ago
Or you can spare yourself the shellout and use
finfo_file
– Federico klez Culloca
1 hour ago
Or you can spare yourself the shellout and use
finfo_file
– Federico klez Culloca
1 hour ago
add a comment |
up vote
-3
down vote
You can check for the filetype with 'filetype()
' in PHP: PHP filetype
New contributor
3
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
add a comment |
up vote
-3
down vote
You can check for the filetype with 'filetype()
' in PHP: PHP filetype
New contributor
3
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
add a comment |
up vote
-3
down vote
up vote
-3
down vote
You can check for the filetype with 'filetype()
' in PHP: PHP filetype
New contributor
You can check for the filetype with 'filetype()
' in PHP: PHP filetype
New contributor
New contributor
answered 1 hour ago
Mirza Mašić
1312
1312
New contributor
New contributor
3
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
add a comment |
3
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
3
3
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
You should check what you linked. "Possible values are fifo, char, dir, block, link, file, socket and unknown."
– Federico klez Culloca
1 hour ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree with @FedericoklezCulloca
– marvinIsSacul
53 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
I agree as well. My bad.
– Mirza Mašić
50 mins ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407649%2fhow-to-check-file-is-valid-image-or-not-using-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why should that be of interest? If a user uploads something, then that is his own problem. Typically you only want to re-deliver that if it is requested. If someone uploaded a text file renamed somehow, then fine, he get's back a text file.
– arkascha
1 hour ago
Have you done anything on your own yet? You are expected to try to write the code yourself. Please read How to create a Minimal, Complete, and Verifiable example.
– kerbholz
1 hour ago
@arkascha that could open your application to security issues.
– Federico klez Culloca
1 hour ago
2
@FedericoklezCulloca Can you explain how that is possible?
– patrick
42 mins ago
@FedericoklezCulloca Only if you actually use such uploaded data and integrate it into your application logic. Which of course no one does specifically because you cannot in any way trust data uploaded from the client side. Yes, you can deliver a file that contains something else than what it appears to hold at first. So what? That is not a "security issue".
– arkascha
34 mins ago