How to Upload Files to a Server Using PHP
This article explains how to create an HTML form and a PHP script to upload files to a server, detailing the required form attributes, handling of the uploaded file on the backend, and displaying success or failure messages.
In this tutorial we demonstrate how to upload files to a server using PHP, covering both the client‑side HTML form and the server‑side processing script.
1. HTML Form
<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>The form uses enctype="multipart/form-data" and posts to service_file.php, allowing the user to select a file and submit it.
2. service_file.php
<?php
echo "<pre>";
print_r($_FILES['file']);
$uploaddir = './upload/'; //存放的路径
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "文件上传成功";
} else {
echo "文件上传失败";
}
?>The script prints the uploaded file information, moves the file to the upload/ directory, and outputs a success or failure message.
When the upload succeeds, the page displays the file details and confirms that the file was stored on the server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
