New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add finfo_close to File::mime #6779
Conversation
im not sure if its even needed as ive seen examples without it but still better safe then sorry, also fixed a bit of logic
What is this supposed to fix? |
if (!$finfo) { | ||
return false; | ||
} | ||
list($type) = explode(';', $finfo); | ||
$mime = finfo_file($finfo, $this->pwd()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading mimetype could fail for some reason, so there should be a check for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah sorry i got mislead by old c habbits of docs reading where finfo_file says it returns string
so full should be like
$finfo = finfo_open(FILEINFO_MIME);
if (!$finfo) {
return false;
}
$mime = finfo_file($finfo, $this->pwd());
if (!$mime) {
finfo_close($finfo);
return false;
}
finfo_close($finfo);
list($type) = explode(';', $mime);
return $type;
or anything else to change you see with your eyes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OOP style usage looks much cleaner:
try {
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file($this->pwd());
if (!$type) {
return false;
}
list($type) = explode(';', $type);
return $type;
} catch (\Exception $e) {
return false;
}
Here I am assuming it throws an exception in case of an error, can't find any docs relating to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont see any throws in:
https://github.com/php/php-src/blob/PHP-5.4.1/ext/fileinfo/fileinfo.c
so looks like all that is needed is
$finfo = new finfo(FILEINFO_MIME);
if (!$finfo ) {
return false;
}
$type = $finfo->file($this->pwd());
if (!$type) {
return false;
}
list($type) = explode(';', $type);
return $type;
as i understand it will autoclose on destroy
Closing the finfo resource makes sense, but the point @ADmad made around error checking should be addressed. |
Superseded by #6810 |
im not sure if its even needed as ive seen examples without it but still better safe then sorry, also fixed a bit of logic