#0x01创建文件上传表单:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename: </label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
#0x02创建文件上传脚本:
<?php
/*
*
* $_FILES['file']['name'] - 被上传文件的名称
* $_FILES['file']['type'] - 被上传的类型
* $_FILES['file']['size'] - 上传文件的大小,以字节计
* $_FILES['file']['tmp_name'] - 存储在服务器的文件的临时副本的名称
* $_FILES['file']['error'] - 由文件上传导致的错误代码
*
* */
if ($_FILES['file']['error'] > 0) {
echo 'Error: ' . $_FILES['file']['error'] . '<br />';
} else {
echo 'upload: ' . $_FILES['file']['name'] . '<br />';
echo 'Type: ' . $_FILES['file']['type'] . '<br />';
echo 'Size: ' . $_FILES['file']['size'] . '<br />';
echo 'Stored in: ' . $_FILES['file']['tmp_name'] . '<br />';
}
回显:
#0x3 上传限制
w3school针对文件的限制,存在问题,只是做了简单的数据类型校验。
<?php
//增加对文件上传的限制,用户只能上传.gif或.jpeg文件, 文件大小必须小于20kb;
if ((($_FILES['file']['type'] == 'image/gif')
|| ($_FILES['file']['type'] == 'image/jpeg')
|| ($_FILES['file']['type'] == 'image/pjpeg')
&& ($_FILES['file']['size'] < 20000))) {
if ($_FILES['file']['error'] > 0) {
echo 'Error' . $_FILES['file']['error'] . '<br />';
} else {
echo 'Upload: ' . $_FILES['file']['name'] . '<br />';
echo 'Type: ' . $_FILES['file']['type'] . '<br />';
echo 'Size: ' . $_FILES['file']['size'] . '<br />';
echo 'Stored in: ' . $_FILES['file']['tmp_name'];
}
} else {
echo 'Invalid file';
}
#0x04 保存文件:
<?php
//增加对文件上传的限制,用户只能上传.gif或.jpeg文件, 文件大小必须小于20kb;
if ((($_FILES['file']['type'] == 'image/gif')
|| ($_FILES['file']['type'] == 'image/jpeg')
|| ($_FILES['file']['type'] == 'image/pjpeg')
&& ($_FILES['file']['size'] < 20000))) {
if ($_FILES['file']['error'] > 0) {
echo 'Return Code: ' . $_FILES['file']['error'] . '<br />';
} else {
echo 'Upload: ' . $_FILES['file']['name'] . '<br />';
echo 'Type: ' . $_FILES['file']['type'] . '<br />';
echo 'Size: ' .($_FILES['file']['size'] / 1024) . 'kb <br />';
echo 'Temp file: ' . $_FILES['file']['tmp_name'] . '<br />';
if (file_exists('upload/' . $_FILES['file']['name'])) {
echo $_FILES['file']['name'] . ' already exists.';
} else {
move_uploaded_file($_FILES['file']['tmp_name'], 'upload/' . $_FILES['file']['name']);
echo 'Stored in: ' . 'upload/' . $_FILES['file']['name'];
}
}
} else {
echo 'Invalid file';
}
#0x05 文件上传漏洞
Content-Type 只校验了文件类型
通过相关资料学习和总结PHP相关上传的一些收集:
①【.php 不输入文件名】
②【x.php. a.phpx x.phtml x.php; x.php~】
③【php后缀+空格】