Home  >  Article  >  Backend Development  >  Implementation principle of PHP upload and download

Implementation principle of PHP upload and download

Guanhui
GuanhuiOriginal
2020-05-13 14:26:532435browse

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[&#39;myfile&#39;][&#39;tmp_name&#39;];
//文件大小
$size = $_FILES[&#39;myfile&#39;][&#39;size&#39;];
//文件类型
$type = $_FILES[&#39;myfile&#39;][&#39;type&#39;];
//文件上传
$error = $_FILES[&#39;myfile&#39;][&#39;error&#39;];

if($_FILES["myfile"]["error"] > 0){
    echo "错误信息".$_FILES["myfile"]["error"];
} else {
    echo $myfile.&#39;<br>&#39;;
    echo $tmp.&#39;<br>&#39;;
    echo $size/1024 .&#39;KB<br>&#39;;
    echo $type.&#39;<br>&#39;;
    echo $error.&#39;<br>&#39;;
}

if(file_exists("./".$_FILES["myfile"]["name"])){
    echo $_FILES["myfile"]["name"] . "   文件存在";
} else {
    move_uploaded_file($_FILES[&#39;myfile&#39;][&#39;tmp_name&#39;],"./". $_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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn