PHP开发札记系列(八)- 上传与下载
PHP开发笔记系列(八)- 上传与下载
??? 最近由于项目需要,在GZBGY进行了闭关,与同行的Developer进行了深入的交流,成果不赖。丢下了《PHP开发笔记系列(XAMPP+PhpEclipse+XDebug)》,今天重新更新一下,这次讲上传下载。
?
??? 上传与下载两个功能是再正常不过的基本功能,在java的开发中,下载会使用File类将文件内容以流的形式写入到response中,并设置相应和http header,让浏览器识别本次是文件下载。在PHP中也是类似,本文《PHP开发笔记系列(八)- 上传与下载》将是《PHP开发笔记系列(XAMPP+PhpEclipse+XDebug)》的第八篇,讲述如何使用PHP完成文件的上传与下载操作。
??? 1. 文件下载
??? 文件下载的操作分为两部分,步骤:1)设置http header,2)读取文件。这里我们编写两个php脚本,第一个为file-list.php,用来显示当前文件夹下的所有文件夹和文件,供下载,第二个为download.php,具体的下载代码。代码如下:
?
file:file-list.phpurl:http://localhost:88/download-upload/file-list.php<?php $location = './'; $dp = opendir($location); while ($entry = readdir($dp)){ if(is_dir($entry)) { echo '[DIR] '.$entry. '<br/>'; }elseif (is_file($entry)) { echo '[FILE] <a href="download.php?filename='.%24entry.'">'.$entry. '</a><br>'; } } closedir($dp); echo "<a href="upload-form.php">upload</a>";?>
?
file:download.phpurl:http://localhost:88/download-upload/download.php?filename=xxx<?php if (isset($_GET['filename'])) { $filename = $_GET['filename']; } else { die('Parameter filename doesn\'t exist!'); } if (!file_exists($filename)) { die($filename.' doesn\'t exist!'); } Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length: ".filesize($filename)); Header("Content-Disposition: attachment; filename=" . $filename); readfile($filename); ?>
??? 2. 文件上传
??? 在PHP中,文件上传非常简单,因为php已经自动识别enctype为"multipart/form-data"的请求,自动将里面的文件域中的内容upload到php的tmp文件夹中,我们可以过$_FILES["文件域名称"]["属性"],如$_FILES["file"]["name"]表示上传文件的原始名称、$_FILES["file"]["type"]表示上传文件的类型、$_FILES["file"]["tmp_name"]表示上传文件在php临时文件夹中的路径。由于生成的临时文件会被清空,因此我们需要使用move_uploaded_file()函数将临时文件移动到我们的指定目录中,其中第一个参数为临时文件的路径,第二个参数为目标文件的路径。代码如下:
?
file:upload-form.phpurl:http://localhost:88/download-upload/upload-form.php
?
file:upload.phpurl:http://localhost:88/download-upload/upload.php<?phpif ($_FILES["file"]["error"] > 0){ echo "Error: ".$_FILES["file"]["error"] . "<br>";}else{ echo "Upload: ".$_FILES["file"]["name"]."<br>"; echo "Type: ".$_FILES["file"]["type"]."<br>"; echo "Size: ".($_FILES["file"]["size"] / 1024)." Kb<br>"; echo "Temp file: ".$_FILES["file"]["tmp_name"]."<br>"; move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]); echo "<a href="file-list.php">file-list</a>";}?>
?? 本文地址:http://ryan-d.iteye.com/blog/1546706
?
?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
