Home > Article > Backend Development > Implementation principle of PHP upload and download
The implementation principle of PHP upload and download
First, the user submits the file to the PHP server, and PHP will automatically temporarily store the file; Then the business code saves the file to the server and returns the file access address; finally, the front end accesses and downloads the file based on the access address.
File upload example
HTML:
<html> <head> <title>简单文件上传</title> </head> <body> <form action="./upload_file.php" method="POST" enctype="multipart/form-data"> <label for="file">文件:</label> <input type="file" name="myfile" id="file"> <button type="submit">上传文件</button> </form> </body> </html>
PHP:
<?php header("content-type:text/html;charset=utf-8"); //获取文件配置信息 $myfile = $_FILES["myfile"]["name"]; //临时目录 $tmp = $_FILES['myfile']['tmp_name']; //文件大小 $size = $_FILES['myfile']['size']; //文件类型 $type = $_FILES['myfile']['type']; //文件上传 $error = $_FILES['myfile']['error']; if($_FILES["myfile"]["error"] > 0){ echo "错误信息".$_FILES["myfile"]["error"]; } else { echo $myfile.'<br>'; echo $tmp.'<br>'; echo $size/1024 .'KB<br>'; echo $type.'<br>'; echo $error.'<br>'; } if(file_exists("./".$_FILES["myfile"]["name"])){ echo $_FILES["myfile"]["name"] . " 文件存在"; } else { move_uploaded_file($_FILES['myfile']['tmp_name'],"./". $_FILES["myfile"]["name"]); echo "文件位置:"."./". $_FILES["myfile"]["name"]; }
Recommended tutorial:《PHP Tutorial》
The above is the detailed content of Implementation principle of PHP upload and download. For more information, please follow other related articles on the PHP Chinese website!