How to use PHP to implement a simple online file upload and download system

WBOY
Release: 2023-09-24 15:14:01
Original
1565 people have browsed it

How to use PHP to implement a simple online file upload and download system

How to use PHP to implement a simple online file upload and download system, specific code examples are needed

In the Internet age, file transmission and sharing are very common needs . Whether you are an individual or a business, you need to handle uploading and downloading files conveniently and quickly. As a powerful server-side scripting language, PHP provides a wealth of functions and tools to easily implement file upload and download functions.

This article will introduce how to use PHP to implement a simple online file upload and download system, and provide detailed code examples.

First, we need to create a folder for file upload and download. In the root directory of the project, create a folder called uploads. This folder will be used to store files uploaded by users.

Next, we need to create a page for file upload. Create a file called upload.php and add the following code in it:

<!DOCTYPE html>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
    <h1>文件上传</h1>
    <form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="上传">
    </form>

<?php
if ($_FILES) {
    $file = $_FILES['file'];

    // 检查上传文件是否成功
    if ($file['error'] === UPLOAD_ERR_OK) {
        $filename = $file['name'];
        $filepath = 'uploads/' . $filename;

        // 将上传的文件移动到目标路径
        if (move_uploaded_file($file['tmp_name'], $filepath)) {
            echo '文件上传成功!';
        } else {
            echo '文件上传失败。';
        }
    }
}
?>

</body>
</html>
Copy after login

The above code creates a form for file upload where the user can select the file to upload and Click the submit button. After submitting the form, the form data will be sent to the upload.php file for processing. In the upload.php file, first check whether a file has been uploaded, and then save the uploaded file to the uploads folder. If the save is successful, a prompt indicating that the file has been successfully uploaded will be displayed. , otherwise a message indicating that the upload failed will be displayed.

Then, we need to create a page for file download. Create a file called download.php and add the following code in it:

<!DOCTYPE html>
<html>
<head>
    <title>文件下载</title>
</head>
<body>
    <h1>文件下载</h1>
    <ul>
    <?php
    $files = scandir('uploads/');
    foreach ($files as $file) {
        if ($file !== '.' && $file !== '..') {
            echo "<li><a href='uploads/$file' download>$file</a></li>";
        }
    }
    ?>
    </ul>
</body>
</html>
Copy after login

The above code reads all the files in the uploads folder and ends with A list is displayed on the page. Users can click on the file name to download the file. Note that you can specify the file name used when downloading by adding the download attribute to the link.

At this point, we have implemented a simple online file upload and download system. Users can select files and upload them on the upload page, and then click on the file name on the download page to download.

It should be noted that in order to ensure the security of file upload and download, we should limit the type and size of uploaded files, and perform permission verification when downloading to ensure that users can only download files they have permission to document.

In summary, it is very simple to implement a simple online file upload and download system using PHP. By using the functions and tools provided by PHP, we can easily implement file upload and download functions, which greatly facilitates file transfer and sharing. Whether you are an individual or a business, you can flexibly apply these functions according to your own needs.

The above is the detailed content of How to use PHP to implement a simple online file upload and download system. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template