1.post-file.html
form表单提交方式一定要是post,而且添加属性enctype="multipart/form-data"
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="post-file.php" method="post" enctype="multipart/form-data"> <input type="file" name="upFile"><br> <input type="submit" value="提交"> </form> </body> </html>
2.post-file/php
默认情况下,文件会被保存到一个临时文件夹下,需要使用move_uploaded_file方法将文件移动到指定文件夹
通过$_FILES获取文件的信息
<?php $fileInfo = $_FILES["upFile"]; $fileInfoName = $fileInfo["name"];//文件名 $fileInfoPath = $fileInfo["tmp_name"];//文件当前路径文件夹 move_uploaded_file($fileInfoPath,"./source/".$fileInfoName);