<?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>