使用 strpos 尝试 运行 基于文件类型的条件

Attempt at running a condition based on file type using strpos

我正在创造条件。条件是立即(在 if 中)检查是否有任何文件不是基本图像文件(png、jpg 等——条件中显示的文件)。如果是这种情况,那么 file_put_contents 应该 运行。如果只有基本图像文件正在上传,那么其他应该 运行.

截至目前,else 永远不会 运行。例如,如果我上传一个 .png 文件,else 语句应该 运行.

当有人添加文件时,我用 $uploadedFileTypes = $fu->getImageFileTypes(); 检查文件类型,然后将其内爆到列表中。这 100% 有效。

这是我想要发生的事情的示例:

如果有人上传两个文件 - .png 和 .stp,那么 if 应该 运行。仅当上传的文件是基本图像文件时,我才希望 else 为 运行。有更好的方法吗?

然后我正在使用 strpos 函数来检查我指定的每种类型的图像文件。

if (strpos($fileTypeString,$pdf) || strpos($fileTypeString,$jpg) || strpos($fileTypeString,$jpeg) || strpos($fileTypeString,$png) || strpos($fileTypeString,$gif) === false) {

有没有人看出我做错了什么?

$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();
$fileTypeString = implode( ", ", $uploadedFileTypes );
$pdf = "pdf";
$jpg = "jpg";
$jpeg = "jpeg";
$png = "png";
$gif = "gif";
file_put_contents('file_type_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r($uploadedFileTypes, true), FILE_APPEND);

foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (!empty($value)) { //empty string
            if ($file['error'][$key] != 4) {
                if (strpos($fileTypeString,$pdf) || strpos($fileTypeString,$jpg) || strpos($fileTypeString,$jpeg) || strpos($fileTypeString,$png) || strpos($fileTypeString,$gif) === false) {
                    file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND);
                } else {
                    $out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). '  uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
                    foreach ($filename as $indFile) {
                        //print_r($template);
                        $out .= "<li><a href='/php/uploads/{$indFile}'>{$indFile}</a></li>";
                    }
                    $out .= '</ul>';
                    $template = str_replace("{filename}", $out, $template);
                }
            } else { //error code IS #4
                //echo "error code is 4";
            }
        } else {
            //echo "name is empty!";
            $template = str_replace("{filename}", '', $template);
        }
    }
}

编辑,使用不同方法的新代码:

$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();
$fileTypeString = implode( ", ", $uploadedFileTypes);
$imageTypes = ["pdf","jpg", "jpeg", "png", "gif"];
$nonImgFiles = false;
$imgFiles = false;

if (!in_array($uploadedFileTypes, $imageTypes)) {
    $nonImgFiles = true;
}

if (in_array($uploadedFileTypes, $imageTypes)) {
    $imgFiles = true;
}

foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (!empty($value)) { //empty string
            if ($file['error'][$key] != 4) {        
                if ($nonImgFiles == true) {
                    file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND);
                } else {
                    }

我可以建议一种检查文件类型的替代方法,而不是处理字符串和检查不同的变量,有一个你想要捕获的类型的数组,然后检查上传的文件是否匹配它们中的任何一个(使用 array_intersect() 在这个例子中)...

$uploadedFileTypes = $fu->getImageFileTypes();
$imagesTypes = ["pdf","jpg", "jpeg", "png", "gif"];

会给你数组进行比较

然后你的测试会是这样的...

if ( empty(array_intersect($uploadedFileTypes, $imagesTypes)) ) {

如果你想确保加载了哪些类型,你可以改变这一轮,这样你就可以开始...

$imagesTypes = ["png","stp"];

然后检查您期望的类型和扩展名之间是否存在任何差异...

if ( !empty(array_diff($uploadedFileTypes, $imagesTypes)) ) {

您必须将 === false 添加到所有条件,并将运算符更改为 &&,例如:

if (
    strpos($fileTypeString,$pdf) === false && 
    strpos($fileTypeString,$jpg) === false && 
    strpos($fileTypeString,$jpeg) === false && 
    strpos($fileTypeString,$png) === false && 
    strpos($fileTypeString,$gif) === false
) {
    //do something    
} else {
   //do something
}

但似乎您可以在 if 中使用更少的代码来实现:

$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
file_put_contents('file_type_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r($uploadedFileTypes, true), FILE_APPEND);

$extensions = ['pdf', 'jpg', 'jpeg', 'png', 'gif']; //an array with your extensions
foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (!empty($value)) { //empty string
            if ($file['error'][$key] != 4) {
                $fileData = pathinfo($file['name']); //explod file path to array
                if (!in_array($fileData['extension'], $extensions)) { //if file extension not in your $extensions array
                    file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND);
                } else {
                    $out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). '  uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
                    foreach ($filename as $indFile) {
                        //print_r($template);
                        $out .= "<li><a href='/php/uploads/{$indFile}'>{$indFile}</a></li>";
                    }
                    $out .= '</ul>';
                    $template = str_replace("{filename}", $out, $template);
                }
            } else { //error code IS #4
                //echo "error code is 4";
            }
        } else {
            //echo "name is empty!";
            $template = str_replace("{filename}", '', $template);
        }
    }
}

编辑:

我重读了你的问题。我相信您需要删除循环外的 文件扩展名 检查。像这样的东西会起作用:

$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();

file_put_contents('file_type_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r($uploadedFileTypes, true), FILE_APPEND);

//your per-file validation loop can go here

$extensions = 'pdf', 'jpg', 'jpeg', 'png', 'gif'];

//fill $differentExtensions with all $uploadedFileTypes elements that 
//does not exist inside $extensions
$differentExtensions = array_diff($uploadedFileTypes, $extensions);
if (count($differentExtensions) > 0) {
    file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND);  
} else {
    $out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). '  uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
    foreach ($filename as $indFile) {
        //print_r($template);
        $out .= "<li><a href='/php/uploads/{$indFile}'>{$indFile}</a></li>";
    }
    $out .= '</ul>';
    $template = str_replace("{filename}", $out, $template);
}

//your per-file validation loop can go here

您用来验证错误的循环将是一个单独的代码块。您可以根据需要将其放在 文件扩展名 检查之前或之后(我在上面的代码中提到了建议的位置作为注释):

foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (empty($value)) {
            //echo "name is empty!";
            $template = str_replace("{filename}", '', $template);
        }
        if ($file['error'][$key] == 4) {
            //echo "error code is 4";
        }
    }
}