检查文件是图像还是pdf

时间:2018-09-04 12:25:16

标签: php laravel

我需要检查文件是php / laravel中的图像还是pdf。

这就是我现在拥有的:

return $file['content-type'] == 'image/*';

除了'image / *',我还需要添加'application / pdf'

如何添加?

更新

更清楚地说,有没有一种方法可以添加更多允许的类型而不必执行OR条件。我现在用in_array得到了答案!

4 个答案:

答案 0 :(得分:5)

我喜欢这种方法,它节省了一些打字

return (in_array($file['content-type'], ['image/jpg', 'application/pdf']));

答案 1 :(得分:3)

您可以简单地使用OR语句,即

return ($file['content-type'] == 'image/*' || $file['content-type'] == 'application/pdf');

这是假设您仍然只想返回true / false。因此,呼叫者将知道该文件是PDF还是图像。

或者您是说return语句必须产生一个区分这两种类型的值?还不清楚。

如果是后者,那么您可能想要更多类似的东西

$type = null;
switch ($file['content-type']) {
  case "image/*":
    $type = "image";
    break;
  case "application/pdf":
    $type = "pdf";
    break;
}
return $type;

答案 2 :(得分:1)

您可以使用OR条件检查文件的内容类型,以添加其他检查条件。

return ($file['content-type'] == 'image/*' || $file['content-type'] == 'application/pdf')

但是最好将所有条件值放在数组中,然后使用in_array检查它们的存在。

return (in_array(['image/jpg', 'application/pdf'], $file['content-type']));

答案 3 :(得分:1)

return $file['content-type'] == 'image/*' || return $file['content-type'] == 'application/pdf';

“ ||”含义或

相关问题