php上传图片到后台

html代码

<!DOCTYPE html>
<html lang="en">
<head></head>

<body class="templatemo-bg-gray">
    <div class="container">
        <div class="col-md-12">
            <form class="form-horizontal templatemo-container margin-bottom-30"  action="action.php" method="post" enctype="multipart/form-data">                
                <div class="form-group 1">
                  <div class="col-xs-12" style="padding:0;">                    
                    <div class="control-wrapper" style="padding:0;">
                    <table class="table table-striped table-bordered table-hover">
                            <tr>
                                <th class="center">图片</th>
                            </tr>
                            <tr >
                                <td class="center">
                                    <input type="file" class="form-control" name="file" placeholder="上传">
                                </td>
                            </tr>
                    </table>
                    </div>                                    
                  </div>              
                </div>
              </form>
        </div>
    </div>
</body>
</html>

其中form表单中enctype="multipart/form-data"必须要有,其中w3c中是这样描述

enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。

默认地,表单数据会编码为 "application/x-www-form-urlencoded"。就是说,在发送到服务器之前,所有字符都会进行编码(空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值)。

上传文件时必须使用该值

php后台

$file = $_FILES["file"];
        if(!empty($_FILES["file"]["tmp_name"])){
            var_dump($FILES["file"]["tmp_name"]["size"]);
            $name = $file['name'];
            $type = strtolower(substr($name,strrpos($name,'.')+1)); 
            $allow_type = array('jpg','jpeg','gif','png'); 
            if(!in_array($type, $allow_type)){
              return ;
            }
            if(!is_uploaded_file($file['tmp_name'])){
              return ;
            }
            $upload_path = "img/"; 
            $name1 = $file['name'];
            $namearr = explode(".",$name1);
            $filename = $namearr[1];
            if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
            rename($upload_path.$file['name'],$upload_path.$filename);
            }else{ }
        }else{
            $imgupload = false;
        }

OK,妥妥的搞定。

转载于:https://my.oschina.net/blr/blog/679725

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值