所以我有这个脚本来上传文件,是的,我知道它是不安全的。但是,我现在的问题是我可以上传名为filename.php.jpg的文件。那么如何防止像theese这样的文件上传呢?我用其他东西阻止了php文件的执行。
<?php
$maindir = "../alla_bilder/images/";
$maindir_th = "../alla_bilder/images/thumbs/";
$uploaddir = "images/";
$uploaddir_th = "images/thumbs/";
$allowed = array('jpg','jpeg','png');
$notallowed = array('php');
$uploadOk = 1;
$max_size = 5048 * 1024;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['size'] <= $max_size)
{
$file_ext = pathinfo($_FILES[$key]['name'],PATHINFO_EXTENSION);
$file_name = basename($_FILES[$key]['name'],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed))
{
$name = $_FILES[$key]['name'];
$x = 1;
while (file_exists($uploaddir.'/'.$name))
{
$name = $file_name.'['.$x.'].'.$file_ext;
$x++;
}
$name = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 22)), 0, 22). '.' .mb_strtolower ($file_ext);
if (move_uploaded_file($_FILES[$key]['tmp_name'],$uploaddir.'/'.$name))
{
chmod($uploaddir.'/'.$name, 0644);
}
else
{
die(error_get_last());
}
}
else
{
die("Invalid file type");
}
}
else
{
die("File size too big");
}
copy($uploaddir.'/'.$name, $maindir.'/'.$name);
$images = glob("images/thumbs/*.*");
foreach($images as $image)
{
$output .= '<div class="col-md-2" align="center" ><img src="' . $image .'" width="200px" height="140px" style="border:1px solid #ccc;" /></div>';
}
//thumbnail image making part
$modwidth = 200;
$modheight = 140;
list($width, $height) = getimagesize($uploaddir.'/'.$name);
$ratio_orig = $width/$height;
if ($width/$height > $ratio_orig)
{
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
$tn = imagecreatetruecolor($modwidth, $modheight);
//$image = imagecreatefromjpeg($uploaddir.'/'.$name);
$image = imagecreatefromstring(file_get_contents($uploaddir.'/'.$name));
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $uploaddir_th.'/'.$name);
imagejpeg($tn, $maindir_th.'/'.$name);
}
echo "STOP!";
?>
答案 0 :(得分:1)
看看finfo
扩展名,这使您可以确定 true 文件类型,因为它会在OS级别上嗅探文件类型。
http://php.net/manual/en/function.finfo-file.php
由于finfo
是扩展名,因此需要安装并启用。
http://php.net/manual/en/fileinfo.installation.php
$path = $_FILES[$key]['tmp_name'],$uploaddir.'/'.$name;
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$whitelist = array('image/jpg');
if (in_array(finfo_file($finfo, $path), $whitelist) && move_uploaded_file($path))
{
chmod($uploaddir.'/'.$name, 0644);
}
答案 1 :(得分:1)
您可以将文件名分解为n个部分,然后检查是否有2个以上的文件名(通常应为name和ext)。 如果是这种情况,请优雅地退出(或不退出)。 您也可以遍历数组以添加额外的扩展名,但使用-或_而不是。
// Split the filename into array of base and extension[s]
$parts = explode('.', $filename);
// If more than two parts, it could be a double ext case
if ( count( $parts ) > 2 ) {
die("Invalid file type");
}
替代解决方案:
// Split the filename into array of base and extension[s]
$parts = explode('.', $filename);
// If more than two parts, it could be a double ext case
if ( count( $parts ) > 2 ) {
$filename = array_shift($parts);
$extension = array_pop($parts);
foreach ($parts as $part) {
$filename .= ‘_’ . $part;
}
}
作为一个旁注,正如您提到的,还需要其他保护机制。最好的方法可能是将这些文件存储在无法执行php的目录中,因为即使有一个看起来正常的.jpg扩展名,恶意代码也总是可以插入图像元数据中。