在Laravel

时间:2017-05-19 00:12:15

标签: php laravel file file-extension

我遇到了一个奇怪的问题。我正在使用File::extension($file),实际上得到的答案是'pdf'。我var_dump() File::extension($file),如果字符串中包含3个字符'pdf',则显示值。

然后我尝试在if语句中对它进行比较,但它会进入if语句,它不应该。这是一种奇怪的行为。

$fileType = File::extension($request->frequencyPlan->getClientOriginalName());

if ($fileType != 'pdf' || $fileType != 'doc') {
    return $this->showEstablishmentsEdit('fileTypeErrorForPDF');
};

我错过了什么吗?

P.S:对于那些想知道的人,我无法使用mimeType验证器,因为我收到了另一个错误

  

'无法猜测mime类型,因为没有可用的猜测器(你启用了php_fileinfo扩展吗?)'

我认为上面的if语句应该可行。

2 个答案:

答案 0 :(得分:3)

试试这个:

$fileType = $request->frequencyPlan->extension();

if ($fileType !== 'pdf' && $fileType !== 'doc') {
  return $this->showEstablishmentsEdit('fileTypeErrorForPDF');
};

以及其他问题:

  

'无法猜测mime类型,因为没有可用的猜测器(你启用了php_fileinfo扩展吗?)'

托管服务器:

  • 与托管服务提供商联系并告诉他启用此扩展程序 php_fileinfo

本地托管:

  • 你的操作系统是什么?

答案 1 :(得分:1)

您的if语句出现逻辑错误。

$fileType等于pdf时,您的if条件仍将评估为true$fileType != 'pdf'将为false,但下半部分$fileType != 'doc'true,并且由于您已将这些条件组合在一起,因此结果为: true

$fileType = 'pdf'
然后$fileType != 'pdf'false 然后$fileType != 'doc'true

因此,($fileType != 'pdf' || $fileType != 'doc') === (false || true) === (true)进入if分支。

我假设你想进入if分支,如果扩展名不是" pdf" 不是" doc"。

您的代码应为:

if ($fileType != 'pdf' && $fileType != 'doc') {
    return $this->showEstablishmentsEdit('fileTypeErrorForPDF');
};