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
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传文件"> </form>
<?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 "文件上传发生错误!"; } ?>
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传文件"> </form>
<?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 "文件上传发生错误!"; } ?>
3. Implementation of 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 "参数错误!"; } ?>
<?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 "参数错误!"; } ?>
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!