- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我将图像文件上传到 php/mysql 服务器的代码。我读过类似的帖子和教程,但大多数人都说它们不完整。
这是否足以确保安全,或者还缺少什么?
if(isset($_FILES["file"]) && $_FILES["file"]['error'] == 0)
{
$file = $_FILES["file"];
$file = $_FILES["file"]['name'];
$file_type = $_FILES["file"]['type'];
$file = strtolower($file);
$file_type = strtolower($file_type);
if(is_array($_FILES["file"]['error'])){
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('Erro: Only one file accepted');
}
$max_file_size = 2097152;
if($_FILES['file']['size'] >= $max_file_size || $_FILES['file']['size'] == 0){
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('Erro: file size exceeded (2mb)');
}
/* check if contain php */
$pos = strpos($file, 'php');
if(!($pos === false)) {
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('Error: image has php script');
}
/* finfo */
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($_FILES['file']['tmp_name']);
$allowed2 = array('jpg'=>'image/jpeg', 'png'=>'image/png', 'jpeg'=>'image/jpeg');
$ext = array_search($mime, $allowed2, true);
if(false === $ext){
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('FileInfo: Extension not allowed');
}
/* get file extension */
$file_ext = strrchr($file, '.');
/* check if it is allowed */
$allowed = array(".jpg",".jpeg",".png");
if (!(in_array($file_ext, $allowed))) {
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('Extension not allowed');
}
/* check upload type */
$pos = strpos($file_type,'image');
if($pos === false) {
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('error 1: Mime type not valid');
}
$imageinfo = getimagesize($_FILES['file']['tmp_name']);
if($imageinfo['mime'] != 'image/jpeg'&& $imageinfo['mime'] != 'image/jpg'&& $imageinfo['mime'] != 'image/png') {
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('error 2: size mime type not valid');
}
//check double file type (image with comment)
if(substr_count($file_type, '/')>1){
echo '<a href="cad_p.php">Try again</a>';
echo "<br/>";
echo "<br/>";
die('error 3: double type');
}
/* upload to upload direcory */
$uploaddir = 'up_files/';
if (file_exists($uploaddir)) {
} else {
mkdir( $uploaddir, 0777);
}
/* change the image name */
$uploadfile = $uploaddir . md5(basename($_FILES['file']['name'])).$file_ext;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "";
} else {
echo "Error moving image file";
echo '<a href="cad_p.php">Try again</a>';
}
$name = $_POST['name'];
if ($insert_stmt = $mysqli->prepare("INSERT INTO products (name, file) VALUES (?, ?)")) {$insert_stmt->bind_param('ss', $name, $uploadfile);
if (! $insert_stmt->execute()) {
echo '<div class="register_success">';
echo "Could not insert...";
echo '</div>';
}else{
echo '<div class="register_success">';
echo "Product Insert success...";
echo '</div>';
}
}
}
else
{
echo '<a href="cad_p.php">Tente novamente</a>';
echo "<br/>";
echo "<br/>";
die('No image file selected');
}
最佳答案
阅读 PHP 手册中的评论后,我从一个名为 sparticvs 的用户那里找到了这个:
"A note of security: Don't ever trust $_FILES["image"]["type"]. It takes whatever is sent from the browser, so don't trust this for the image type. I recommend using finfo_open (http://www.php.net/manual/en/function.finfo-open.php) to verify the MIME type of a file. It will parse the MAGIC in the file and return it's type...this can be trusted (you can also use the "file" program on Unix, but I would refrain from ever making a System call with your PHP code...that's just asking for problems)."
转到此处并向下滚动以查看他的评论: http://php.net/manual/en/reserved.variables.files.php
如果您打算使用该解决方案,那么这里有一些建议:
使用 strip_tag() 这是在有人以某种方式设法做到的情况下将一些 HTML 或 PHP 标记添加到您的代码中。在这里阅读更多:http://php.net/manual/en/function.strip-tags.php
$name = $_POST['name'];这看起来很危险。你根本没有做任何检查。我会再次推荐 strip_tags 并进行类型验证。
编辑:我会将老师的笔记添加到这个答案中:
关于PHP 安全图像上传 - 附加代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338352/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!