Home >Backend Development >PHP Tutorial >Detailed explanation of file upload in PHP (with code examples)

Detailed explanation of file upload in PHP (with code examples)

autoload
autoloadOriginal
2021-04-22 13:40:556607browse

Detailed explanation of file upload in PHP (with code examples)

PHPIn actual combat, users often need to upload files to the server for storage. This article will take you to see how to upload files to the server. middle.

1.HTML file

<html>
<body>

<form action="service_file.php" method="post" enctype="multipart/form-data">
<label for="file">待上传文件名:</label>
<input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="提交" />
</form>

</body>
</html>

Detailed explanation of file upload in PHP (with code examples)

##2.service_file.php

<?php
echo "<pre class="brush:php;toolbar:false">";
print_r($_FILES[&#39;file&#39;]);

$uploaddir = &#39;./upload/&#39;;//存放的路径
$uploadfile = $uploaddir . basename($_FILES[&#39;file&#39;][&#39;name&#39;]);

if (move_uploaded_file($_FILES[&#39;file&#39;][&#39;tmp_name&#39;], $uploadfile)) {
    echo "文件上传成功";
} else {
    echo "文件上传失败";
}

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Detailed explanation of file upload in PHP (with code examples). 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