未加星标

PHP实现的文件操作类及文件下载功能示例

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

本文实例讲述php实现的文件操作类及文件下载功能。分享给大家供大家参考,具体如下:

文件操作类:

<?php
// Copyright 2005, Lee Babin ([email protected])
// 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数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHP数据Head数据库
分页:12
转载请注明
本文标题:PHP实现的文件操作类及文件下载功能示例
本站链接:http://www.codesec.net/view/524338.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(99)