首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -条件语句在使用pathinfo()提取pathinfo['extension']并重命名所有上载时不起作用

PHP -条件语句在使用pathinfo()提取pathinfo['extension']并重命名所有上载时不起作用
EN

Stack Overflow用户
提问于 2014-10-25 23:04:27
回答 1查看 404关注 0票数 0

我已经检查了论坛,找不到任何符合我的问题是什么。即使在我打字的时候,我也读过这些文章。

这可能会变得有点复杂,所以我将尽可能容易地分解它。感谢任何想要承担这个任务的人..。

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

代码语言:javascript
复制
$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;   
    }
}

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

代码语言:javascript
复制
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';

以及公开的方法:

代码语言:javascript
复制
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;
}

}

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

条件看起来很好,并声明是否存在pathinfo' extension‘和typeChecking是off且后缀不是空的,如果是的话,那么如果这个后缀在不可信的列表或空中-是它唯一应该添加扩展的时候。

但是它在每一个好的上传上都添加了后缀。

有人能帮我指点一下我可能做错了什么吗?我希望我已经解释了我的问题,因为我很困惑。我会尽我所能回答所有问题。

感谢那些花时间帮忙的人。

干杯!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-26 00:45:01

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26568159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档