php文件上传的实现过程,下面以实例进行演示:
实例
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="542488"> <fieldset> <legend align="center">文件上传</legend> <p><strong>选择文件:</strong><input type="file" name="upload" id="upload"></p> </fieldset> <p align="center"><button type="submit" name="submit" >上传</button></p> </form> <?php if($_SERVER['REQUEST_METHOD']=='POST'){ if(isset($_FILES['upload'])){ $allow = ['image/jpg','image/jpeg','image/png']; if(in_array($_FILES['upload']['type'],$allow)){ if(move_uploaded_file($_FILES['upload']['tmp_name'],"upload/{$_FILES['upload']['name']}")){ echo "<script>alert('文件上传成功')</script>"; } }else{ echo "<script>alert('仅允许上传jpg和png格式的图片')</script>"; } } if($_FILES['upload']['error']>0){ echo '上传错误'; } } ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行结果:
总结:
$_FILES['file']['name']: 文件原始名称
$_FILES['file']['type']: 文件类型
$_FILES['file']['size']: 上传的文件大小
$_FILES['file']['tmp_name']: 服务器上的临时文件夹
$_FILES['file']['error']: 上传错误代码
$_SERVER['PHP_SELF']:当前php脚本
enctype="multipart/form-data" :允许通过表单上传文件
method:请求类型必须是POST