我想让我的网站防黑客攻击,所以这就是我这么做的原因:
Text: mysql_real_escape_string($myVar);
Number: (int)$myVar;我应该使用类似于$myVar = $_FILE['myFile'];提供的文件数组的东西吗?
发布于 2011-02-06 01:25:55
取决于用例。例如,如果您将filename保存到DB,则应将其转义为字符串。此外,您还应该防止上传和执行PHP脚本。
发布于 2011-02-06 01:33:49
清理文件名是非常重要的。
还有一些你可能想要涵盖的问题,例如,不是Windows中所有允许的字符都允许在*nix中使用,反之亦然。文件名还可能包含相对路径,并可能覆盖其他未上载的文件。
此上传函数取自here
function Upload($source, $destination, $chmod = null)
{
$result = array();
$destination = self::Path($destination);
if ((is_dir($destination) === true) && (array_key_exists($source, $_FILES) === true))
{
if (count($_FILES[$source], COUNT_RECURSIVE) == 5)
{
foreach ($_FILES[$source] as $key => $value)
{
$_FILES[$source][$key] = array($value);
}
}
foreach (array_map('basename', $_FILES[$source]['name']) as $key => $value)
{
$result[$value] = false;
if ($_FILES[$source]['error'][$key] == UPLOAD_ERR_OK)
{
$file = ph()->Text->Slug($value, '_', '.');
if (file_exists($destination . $file) === true)
{
$file = substr_replace($file, '_' . md5_file($_FILES[$source]['tmp_name'][$key]), strrpos($value, '.'), 0);
}
if (move_uploaded_file($_FILES[$source]['tmp_name'][$key], $destination . $file) === true)
{
if (self::Chmod($destination . $file, $chmod) === true)
{
$result[$value] = $destination . $file;
}
}
}
}
}
return $result;
}重要的部分是:
1)确保文件不包含任何相对路径。
2)ph()->Text->Slug(),这确保文件名中只允许.0-9a-zA-Z,所有其他字符都用下划线(_)替换
3)md5_file(),如果已经存在同名的另一个文件,则将其添加到文件名中
看看它的explained here有多好
https://stackoverflow.com/questions/4908321
复制相似问题