前言:
学习php中
1.表单代码:
<html> <head> <title>文件上传</title> </head> <body background-color="blue"> <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <b>文件上传</b> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="提交"> </form> </body> </html>
2。PHP代码
<?php $gsx=array("jpg","gif","txt"); $irs=explode(".",$_FILES['file']['name']); $sx=end($irs); if (($_FILES['file']['size'] < 2000000) && (in_array($sx,$gsx))) { if ($_FILES['file']['error'] > 0) { echo "错误:".$_FILES['file']['error']; } else { if file_exists("upload",$_FILES['file']['name']); { echo "文件已存在"; } else { echo "文件类型:".$_FILES['file']['type']."<br>"; echo "文件字节:".$_FILES['file']['size']."<br>"; echo "文件名:".$_FILES['file']['name']."<br>"; echo "上传目录:upload/"; } } } else { echo "非法文件格式!"; } ?>
笔记:
上传表单: <html> <head> <title>文件上传</title> </head> <body background-color="blue"> <form action="cs.php" method="POST" enctype="multipart/form-data"> <b>文件上传</b> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="提交"> </form> </body> </html> file用于文件上传 enctype用于提交表单时用于什么内容类型(multipart/form-data) 上传的代码: <?php function fisle() { if($_FILES['file']['error']>0) { echo "错误:".$_FILES['file']['error']."<br>"; } else { echo "上传文件名:".$_FILES['file']['name']."<br>"; echo "上传文件类型:".$_FILES['file']['type']."<br>"; echo "文件大小:".$_FILES['file']['size']."<br>"; echo "储存位置:".$_FILES['file']['tmp_name']; } } fisle(); ?> 通过PHP的FILES向服务器传文件 $_FILES['file']['type'] type获取文件上传类型 $_FILES['file']['error'] file 第一个是表单的参数 error是上传报错 $_FILES['file']['name'] name上传文件名称 $_FILES['file']['size'] size是上传的总字节 $_FILES['file']['tmp_name'] 上传文件存放目录 自定义上传目录: test |-----upload # 文件上传的目录 |-----form.html # 表单文件 |-----upload_file.php # php 上传代码 php文件上传总结 如果只是直接判断上传php文件的时候没有 出错的时候就会造成安全问题 所以我们要进行白名单限制 <?php $allowed=array('gif','jpg','png'); $temp=explode(".",$_FILES['file']['name']); echo "上传字节数:".$_FILES['file']['size']; echo "<br>"; $extesion=end($temp); if (($_FILES['file']['type']=="image/gif") || ($_FILES['file']['type']=="image/jpeg") || ($_FILES['file']['type']=="image/jpg") || ($_FILES['file']['type']=="image/pjeg") || ($_FILES['files']['type']=="image/x-png") || ($_FILES['file']['type']=="image/png") && ($_FILES['file']['size'] < 2048000) && in_array($extesion,$allowed)) { if ($_FILES['file']['error'] > 0) { echo "出错:".$_FILES['file']['error']."<br>"; } else { echo "<table border=1>"; echo "<tr>"; echo "<td>"."上传文件名:".$_FILES['file']['error']."</td>"."<br>"; echo "<td>"."上传文件类型:".$_FILES['file']['type']."</td>"."<br>"; echo "</tr>"; echo "</table>"; } if (file_exists("upload/".$_FILES['file']['name'])) { echo '文件已存在'; } else { move_uploaded_file($_FILES['file']['tmp_name'],"upload/".$_FILES['file']['name']); echo "<br>"; echo "<table border=1>"; echo "<tr>"; echo "<td>"."上传文件路径:"."upload/".$_FILES['file']['tmp_name']."</td>"."<br>"; echo "</tr>"; echo "</table>"; } } else { echo '非法文件格式!!!'; } ?> file_exists("文件路径",'$_FILES['file']['name']') 用于判断文件是否存在 move_uploaded_file(指定文件,"目录") 用于指定保存目录