php上传文件验证

<?php 
function upload(){
    GLOBAL $message;
    //先判断这个文件在不在
    if(!isset($_FILES['avatar'])){
        $message= '请上传文件';
        return;
    }
    //接收输出的这个文件信息数组
    $avatar=$_FILES['avatar'];
    //文件没有上传到服务端
    if($avatar['error']!==UPLOAD_ERR_OK){
        $message='文件上传失败';
        return;
    }
    //接收到了文件
    //将文件转入到服务器的文件根目录之内
    $source=$avatar['tmp_name'];//源文件在哪
    //移动的目标路径中文件夹一定是一个已经存在的目录
    $distination='./uploads/'.$avatar['name'];//目的文件在哪
    //这个返回的是一个布尔值
    $moved=move_uploaded_file($source, $distination);
    if(!$moved){
        $message='文件上传失败';
        return;
    }
};



//array(1) {
//  ["avatar"]=>
//  array(5) {
//    ["name"]=>
//    string(24) "仿站小工具 V3.0.exe"
//    ["type"]=>
//    string(24) "application/x-msdownload"
//    ["tmp_name"]=>
//    string(27) "C:\Windows\Temp\php303A.tmp"
//    ["error"]=>
//    int(0)
//    ["size"]=>
//    int(402944)
//  }
//}
if($_SERVER['REQUEST_METHOD']=='POST'){
    upload();
    //var_dump($_FILES);
}
 ?>					
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF'] ;?>" method="post" enctype="multipart/form-data">
        <input type="file" name="avatar">
        <?php if (isset($message)): ?>
            <p><?php echo $message; ?></p>
        <?php endif ?>
        <button>提交</button>
    </form>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值