Home > Backend Development > PHP Tutorial > How to use PHP arrays to implement file upload and download functions

How to use PHP arrays to implement file upload and download functions

王林
Release: 2023-07-16 10:18:02
Original
1428 people have browsed it

How to use PHP arrays to implement file upload and download functions

1. Overview
File upload and download is one of the common functions in Web applications. PHP array is a very useful data structure that can be of great help when implementing file upload and download functions. This article will introduce how to use PHP arrays to implement file upload and download functions, and attach corresponding code examples.

2. Implementation of the file upload function

  1. Create a form
    First, you need to create an HTML form to allow users to select the files to upload. The form requires an input element and sets its type attribute to "file".
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传文件">
</form>
Copy after login
Copy after login
  1. Handling file uploads
    On the server side, you need to create a PHP file to handle file uploads. First, you need to obtain the relevant information of the uploaded file through the $_FILES array. The file can then be moved from the temporary directory to the specified directory via the move_uploaded_file function.
<?php
if ($_FILES["file"]["error"] == 0) {
    $filename = $_FILES["file"]["name"];
    $tempname = $_FILES["file"]["tmp_name"];
    $destination = "uploads/" . $filename;
    
    if (move_uploaded_file($tempname, $destination)) {
        echo "文件上传成功!";
    } else {
        echo "文件上传失败!";
    }
} else {
    echo "文件上传发生错误!";
}
?>
Copy after login
Copy after login
  1. Complete example
    The following is a complete sample code for the file upload function:
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传文件">
</form>
Copy after login
Copy after login
<?php
if ($_FILES["file"]["error"] == 0) {
    $filename = $_FILES["file"]["name"];
    $tempname = $_FILES["file"]["tmp_name"];
    $destination = "uploads/" . $filename;
    
    if (move_uploaded_file($tempname, $destination)) {
        echo "文件上传成功!";
    } else {
        echo "文件上传失败!";
    }
} else {
    echo "文件上传发生错误!";
}
?>
Copy after login
Copy after login

3. Implementation of the file download function

  1. Create a file list
    First, the list of files to be downloaded needs to be displayed on the web page to allow users to select the files to download. This can be achieved by storing the file name in a PHP array and dynamically generating a hyperlink on the web page.
<?php
$files = array_diff(scandir("uploads/"), array(".", ".."));

foreach ($files as $file) {
    echo "<a href='download.php?file=$file'>$file</a><br>";
}
?>
Copy after login
  1. Processing file downloads
    On the server side, a PHP file needs to be created to handle file downloads. First, you need to get the file name to be downloaded through the $_GET array and send the file to the browser.
<?php
if (isset($_GET["file"])) {
    $filename = $_GET["file"];
    $filepath = "uploads/" . $filename;
    
    if (file_exists($filepath)) {
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Content-Type: application/octet-stream");
        header("Content-Length: " . filesize($filepath));
        readfile($filepath);
    } else {
        echo "文件不存在!";
    }
} else {
    echo "参数错误!";
}
?>
Copy after login
  1. Complete example
    The following is a complete sample code for the file download function:
<?php
$files = array_diff(scandir("uploads/"), array(".", ".."));

foreach ($files as $file) {
    echo "<a href='download.php?file=$file'>$file</a><br>";
}
?>

<?php
if (isset($_GET["file"])) {
    $filename = $_GET["file"];
    $filepath = "uploads/" . $filename;
    
    if (file_exists($filepath)) {
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Content-Type: application/octet-stream");
        header("Content-Length: " . filesize($filepath));
        readfile($filepath);
    } else {
        echo "文件不存在!";
    }
} else {
    echo "参数错误!";
}
?>
Copy after login

IV. Summary
By using PHP array , we can implement file upload and download functions simply and efficiently. In file upload, obtain file-related information through the $_FILES array, and use the move_uploaded_file function to move the file to the specified directory. In file download, obtain the file name to be downloaded through the $_GET array, and send the corresponding file to the browser. I hope the sample code in this article will be helpful to you and can easily implement file upload and download functions.

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

Related labels:
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