0

我检查了论坛,找不到与我的问题相符的任何内容。即使在我输入标题时,我也会阅读文章。
这可能会变得有点复杂,所以我将尽可能容易地分解它。感谢任何想要接受这个的人......

我的目标是能够安全地上传文件并使用无用的扩展名重命名具有潜在危险的文件。正在发生的事情是每次通过的上传都将 $suffix 添加到其中,即使这是包含我的条件语句的方法:

$nameparts = pathinfo($nospaces);
        $extension = isset($nameparts['extension']) ?  $nameparts['extension'] : '';
        if (!$this->typeCheckingOn && !empty($this->suffix)){
            if (in_array($extension, $this->notTrusted) || empty($extention)){
                $this->newName = $nospaces . $this->suffix; 
            }


protected function moveFile($file)
{
    $result = $file['name']. ' was uploaded successfully';  
    if (!is_null($this->newName)){
        $result .= ', and was renamed ' . $this->newName;   
    }
}

但这是重要代码的完整细分(除了我缺少的代码)

protected $permittedTypes = array(
            'image/jpeg',
            'image/pjpeg',
            'image/gif',
            'image/png',
            'image/webp',
);
protected $newName;
protected $typeCheckingOn = true;
protected $notTrusted = array ('bin', 'cgi','exe','js','pl','php', 'py', 'sh');
protected $suffix = '.upload';

和公共方法:

public function allowAllTypes($suffix = null) 
{
    $this->typeCheckingOn = false;
    if(!is_null($suffix)) {
        if (strpos($suffix, '.') === 0 || $suffix == '') {
            $this->suffix = $suffix;    
        }else {
            $this->suffix = ".$suffix"; 
        }
    }

}

public function upload()
{
    $uploaded = current($_FILES);   
    if($this->checkFile($uploaded)){  
        $this->moveFile($uploaded);
    }
}
public function getMessages()
{
    return $this->messages;     
}

protected function checkFile($file) 
{
    if ($file['error'] !=0){
        $this->getErrorMessage($file);
        return false;
    }
    if (!$this->checkSize($file)){
        return false;   
    }
    if ($this->typeCheckingOn){
        if (!$this->checkType($file)){
            return false;
        }
    }
    $this->checkName($file);
    return true;
}

protected function checkType($file)
{
    if (in_array($file['type'], $this->permittedTypes)){
        return true ;
    } else{
        $this->messages[] = $file['name'] . ' is not a permitted type of file.';
        return false;
    }


}

protected function checkName($file)
{
    $this->newName = NULL;
    $nospaces = str_replace(' ', '_', $file['name']);   
        if ($nospaces != $file['name']){
                $this->newName = $nospaces;
        }
        $nameparts = pathinfo($nospaces);
        $extension = isset($nameparts['extension']) ?  $nameparts['extension'] : '';
        if (!$this->typeCheckingOn && !empty($this->suffix)){
            if (in_array($extension, $this->notTrusted) || empty($extention)){
                $this->newName = $nospaces . $this->suffix; 
            }
        }
}

protected function moveFile($file)
{
    $result = $file['name']. ' was uploaded successfully';  
    if (!is_null($this->newName)){
        $result .= ', and was renamed ' . $this->newName;   
    }
    $result .= '.';
    $this->messages[] = $result;
}

}

就像我说的,所有通过其他检查的文件都会上传。如果它在列表中,它可以识别错误的文件并停止它,但它会用 $ 后缀重命名每个好的文件。

条件看起来不错,并声明 IF 存在 pathinfo['extension'] AND typeChecking 已关闭且后缀不为空,如果是这样,那么如果该后缀在不受信任的列表中或为空 - 是唯一一次应该添加扩展名。
但它会在每个好的上传文件上添加后缀。

有人可以帮助指导我可能做错了什么吗?我希望我已经解释了我的问题而不会造成混淆。我会尽力回答每一个问题。

感谢任何花时间提供帮助的人。

干杯!

4

1 回答 1

0

empty($extention) <- 扩展名拼写不同

于 2014-10-26T00:45:01.907 回答