这篇文章主要介绍了PHP实现的文件操作类及文件下载功能,结合实例形式分析了php针对文件的读、写、创建及下载等功能实现技巧,需要的朋友可以参考下
具体如下:
文件操作类:
<?php // Copyright 2005, Lee Babin (lee@thecodeshoppe.com) // This code may be used and redistributed without charge // under the terms of the GNU General Public // License version 2.0 or later -- www.gnu.org // Subject to the retention of this copyright // and GPL Notice in all copies or derived works class cfile { //The path to the file we wish to work with. protected $thepath; //Error messages in the form of constants for ease of use. const FOUNDERROR = "Sorry, the file in question does not exist."; const PERMERROR = "Sorry, you do not have the proper permissions on this file"; const OPENERROR = "Sorry, the file in question could not be opened."; const CLOSEERROR = "Sorry, the file could not be closed."; //The constructor function. public function __construct (){ $num_args = func_num_args(); if($num_args > 0){ $args = func_get_args(); $this->thepath = $args[0]; } } //A function to open the file. private function openfile ($readorwrite){ //First, ensure the file exists. try { if (file_exists ($this->thepath)){ //Now, we need to see if we are reading or writing or both. $proceed = false; if ($readorwrite == "r"){ if (is_readable($this->thepath)){ $proceed = true; } } elseif ($readorwrite == "w"){ if (is_writable($this->thepath)){ $proceed = true; } } else { if (is_readable($this->thepath) && is_writable($this->thepath)){ $proceed = true; } } try { if ($proceed){ //We can now attempt to open the file. try { if ($filepointer = fopen ($this->thepath, $readorwrite)){ return $filepointer; } else { throw new exception (self::OPENERROR); return false; } } catch (exception $e) { echo $e->getmessage(); } } else { throw new exception (self::PERMERROR); } } catch (exception $e) { echo $e->getmessage(); } } else { throw new exception (self::FOUNDERROR); } } catch (exception $e) { echo $e->getmessage(); } } //A function to close a file. function closefile () { try { if (!fclose ($this->thepath)){ throw new exception (self::CLOSEERROR); } } catch (exception $e) { echo $e->getmessage(); } } //A function to read a file, then return the results of the read in a string. public function read () { //First, attempt to open the file. $filepointer = $this->openfile ("r"); //Now, return a string with the read data. if ($filepointer != false){ //Then we can read the file. return fgets ($filepointer); } //Lastly, close the file. $this->closefile (); } //A function to write to a file. public function write ($towrite) { //First, attempt to open the file. $filepointer = $this->openfile ("w"); //Now, return a string with the read data. if ($filepointer != false){ //Then we can read the file. return fwrite ($filepointer, $towrite); } //Lastly, close the file. $this->closefile (); } //A function to append to a file. public function append ($toappend) { //First, attempt to open the file. $filepointer = $this->openfile ("a"); //Now, return a string with the read data. if ($filepointer != false){ //Then we can read the file. return fwrite ($filepointer, $toappend); } //Lastly, close the file. $this->closefile (); } //A function to set the path to a new file. public function setpath ($newpath) { $this->thepath = $newpath; } } ?>
<?php $myfile = new cfile ("test.txt"); //Now, let's try reading it. echo $myfile->read(); //Then let's try writing to the file. $myfile->write ("Hello World!"); //Then, let's try appending. $myfile->append ("Hello Again!"); ?>
文件下载:
<?php $filename = 'file1.txt'; $file = fopen($filename, 'r'); Header("Expires: 0"); Header("Pragma: public"); Header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); Header("Cache-Control: public"); Header("Content-Length: ". filesize($filename)); Header("Content-Type: application/octet-stream"); Header("Content-Disposition: attachment; filename=".$filename); readfile($filename); ?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
以上是PHP实现的文件操作类及文件下载功能的详细内容。更多信息请关注PHP中文网其他相关文章!

如何使用PHP函数进行邮件发送和接收的附件上传和下载?随着现代通信技术的迅猛发展,电子邮件已成为人们日常沟通和信息传递的重要途径。在Web开发中,经常会遇到需要发送和接收带有附件的邮件的需求。PHP作为一种强大的服务器端脚本语言,提供了丰富的函数和类库,可以简化邮件的处理过程。本文将介绍如何使用PHP函数进行邮件发送和接收的附件上传和下载。邮件发送首先,我们

如何使用Hyperf框架进行文件下载引言:在使用Hyperf框架开发Web应用程序时,文件下载是一个常见的需求。本文将介绍如何使用Hyperf框架进行文件下载,包括具体的代码示例。一、准备工作在开始之前,确保你已经安装好了Hyperf框架并成功创建了一个Hyperf应用程序。二、创建文件下载控制器首先,我们需要创建一个控制器来处理文件下载的请求。打开终端,进

现如今,许多应用程序允许用户进行文件的上传和下载。例如,抄袭检测工具允许用户上传一个包含一些文本的文档文件。然后,它会检查抄袭并生成报告,用户可以下载该报告。每个人都知道使用inputtypefile来创建一个上传文件按钮,但是很少有开发者知道如何使用JavaScript/JQuery来创建一个文件下载按钮。本教程将教授点击HTML按钮或JavaScript时触发文件下载的各种方法。使用HTML的<a>标签和download属性,在按钮点击时触发文件下载每当我们给<a>标

如何在PHP后端功能开发中实现文件上传与下载?在Web开发中,文件上传和下载是非常常见的功能。无论是用户上传图片、文档还是下载文件,都需要后端代码来处理。本文将介绍如何在PHP后端实现文件上传和下载功能,并附上具体的代码示例。一、文件上传文件上传是指将本地电脑中的文件传输到服务器上。PHP提供了丰富的函数和类来实现文件上传功能。创建HTML表单首先,在HTM

CakePHP中间件:实现文件上传和下载功能随着互联网的发展,文件上传和下载功能越来越常见。在开发Web应用程序时,我们经常需要实现文件上传和下载。而在使用CakePHP框架开发应用程序时,中间件是一个非常有用的工具,可以帮助我们简化代码并实现文件上传和下载功能。接下来,我将介绍如何使用CakePHP中间件来实现文件上传和下载功能。首先,我们需要创建一个新的

PHP和CGI的文件上传和下载技术:如何实现文件管理功能简介:文件上传和下载是现代Web应用程序中常见的功能之一。本文将介绍如何使用PHP和CGI编程语言实现文件上传和下载功能,并展示一些代码示例来演示如何管理上传和下载的文件。以下是我们将要涵盖的内容:文件上传的基本概念PHP实现文件上传CGI实现文件上传文件下载的基本概念PHP实现文件下载CGI实现文件下

PHP是一种广泛应用于Web开发的编程语言,它的特点是简单易学、扩展性强、开发周期短,因此广受开发人员的喜爱。在Web开发中,文件上传和下载是一个常见的需求,而PHP提供了一些内置函数和类,帮助我们方便地实现这些功能。本文将介绍PHP中的文件上传和下载技术。一、文件上传技术HTML表单在HTML中,我们可以使用input标签的type属性为“file”来创建

CodeIgniter中间件:提供安全的文件上传和下载功能引言:在Web应用程序开发过程中,文件上传和下载是非常常见的功能。然而,对于安全性的考虑,处理文件上传和下载通常需要额外的安全措施。CodeIgniter是一个流行的PHP框架,提供了丰富的工具和库来支持开发者构建安全可靠的Web应用程序。本文将介绍如何使用CodeIgniter中间件来实现安全的文件


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版
视觉化网页开发工具