我正在使用此代码上传多个图片。我想添加图像清理代码以防止任何攻击。我还想使用md5哈希算法将上传的文件重命名为唯一名称。我该如何做这些事情..请帮帮我......
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
if(isset($_FILES['file']))
{
$count = 0;
$errors= array();
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name )
{
$file_name = $key.$_FILES['file']['name'][$key];
$file_size =$_FILES['file']['size'][$key];
$file_tmp =$_FILES['file']['tmp_name'][$key];
$file_type=$_FILES['file']['type'][$key];
$size = getimagesize($_FILES['file']['tmp_name'][$key]);
if ($size === FALSE) {
die("Oopz,This is not an image");
}
$enc_id= $_POST['form_id'].$_POST['name3'];
$md5folder = md5($enc_id);
$upload_path ="uploads/".$md5folder;
if(!is_dir($upload_path))
{
mkdir($upload_path, 0777, true);
}
if(empty($errors)==true)
{
move_uploaded_file($file_tmp,$upload_path.'/'.$file_name);
}
}
?>
此外,我想知道这部分代码是否有意义?
$size = getimagesize($_FILES['file']['tmp_name'][$key]);
if ($size === FALSE) {
die("Oopz,This is not an image");
}
答案 0 :(得分:0)
使用它来清理数据库输入:
function cleanInput($input) {
$search = array(
'@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
);
$output = preg_replace($search, '', $input);
return $output;
}
?>
<?php
function sanitize($input) {
if (is_array($input)) {
foreach($input as $var=>$val) {
$output[$var] = sanitize($val);
}
}
else {
if (get_magic_quotes_gpc()) {
$input = stripslashes($input);
}
$input = cleanInput($input);
$output = mysql_real_escape_string($input);
}
return $output;
}
使用此选项仅计算文件名的md5哈希值:
$image = "image1.jpg";
$filehash = md5($image );