我已经检查了论坛,找不到任何符合我的问题是什么。即使在我打字的时候,我也读过这些文章。
这可能会变得有点复杂,所以我将尽可能容易地分解它。感谢任何想要承担这个任务的人..。
我的目标是能够安全地上传文件并用无用的扩展名重命名潜在危险的文件。正在发生的事情是,每一次传递的上传都是将$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;
}}
就像我说的,所有通过其他检查的文件都会上传。它可以识别错误的文件,如果它在列表中,就停止它,但是It用$suffix重命名每个好文件。
条件看起来很好,并声明是否存在pathinfo' extension‘和typeChecking是off且后缀不是空的,如果是的话,那么如果这个后缀在不可信的列表或空中-是它唯一应该添加扩展的时候。
但是它在每一个好的上传上都添加了后缀。
有人能帮我指点一下我可能做错了什么吗?我希望我已经解释了我的问题,因为我很困惑。我会尽我所能回答所有问题。
感谢那些花时间帮忙的人。
干杯!
发布于 2014-10-26 00:45:01
空($extention) <-扩展名拼写不同
https://stackoverflow.com/questions/26568159
复制相似问题