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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer























  • Or you can spare yourself the shellout and use finfo_file
    – Federico klez Culloca
    1 hour ago


















up vote
-3
down vote













You can check for the filetype with 'filetype()' in PHP: PHP filetype






share|improve this answer








New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer























  • Or you can spare yourself the shellout and use finfo_file
    – Federico klez Culloca
    1 hour ago















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.






share|improve this answer























  • Or you can spare yourself the shellout and use finfo_file
    – Federico klez Culloca
    1 hour ago













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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
















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












up vote
-3
down vote













You can check for the filetype with 'filetype()' in PHP: PHP filetype






share|improve this answer








New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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















up vote
-3
down vote













You can check for the filetype with 'filetype()' in PHP: PHP filetype






share|improve this answer








New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 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













up vote
-3
down vote










up vote
-3
down vote









You can check for the filetype with 'filetype()' in PHP: PHP filetype






share|improve this answer








New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









You can check for the filetype with 'filetype()' in PHP: PHP filetype







share|improve this answer








New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 1 hour ago









Mirza Mašić

1312




1312




New contributor




Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mirza Mašić is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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




    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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks