douye1940 2013-05-16 04:05
浏览 34

PHP - 文件上传安全性,为什么这个方法不起作用?

I'm new to file uploading, and I realize that security is HUGE. Now per testing purposes before I really wanted to dive into the security I wanted to just get a quick test upload going, when I tried the following "security"

<?PHP
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];

if(isset($name)) {
    if (!empty($name)) {
    $location = "ufiles/";  
    if($_FILES['file']['type'] == "images/jpeg" || $_FILES['file']['type'] == "images/png"){

    move_uploaded_file($tmp_name, $location.$name);
                }
            }
        }
?>

This code does not appear to work, and the file will not upload unless I take off the file type check point. So it's obviously my issue. But why does this not work?

also: I realize it's not a very effective way of securing file types, but it seems most simply logical, and I'm just very confused why it doesn't work.

Edit ALWAYS CHECK YOUR SYNTAX. lol

if($_FILES['file']['type'] == "images/jpeg" || $_FILES['file']['type'] == "images/png")   
  • 写回答

4条回答 默认 最新

  • doumen1883 2013-05-16 04:07
    关注

    It's "image/jpeg" not "images/jpeg". Same goes for the PNG type.

    $_files should also be $_FILES as mentioned by Oberst & Dagon. PHP variables are case-sensitive.

    Like so:

    if(isset($name)) {
        if (!empty($name)) {
        $location = "ufiles/";  
        if($_FILES['file']['type'] == "image/jpeg" || $_FILES['file']['type'] == "image/png"){
    
        move_uploaded_file($tmp_name, $location.$name);
                    }
                }
            }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序