《PHP編程:PHP實(shí)現(xiàn)的文件操作類及文件下載功能示例》要點(diǎn):
本文介紹了PHP編程:PHP實(shí)現(xiàn)的文件操作類及文件下載功能示例,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)本文實(shí)例講述了PHP實(shí)現(xiàn)的文件操作類及文件下載功能.分享給大家供大家參考,具體如下:
PHP學(xué)習(xí)文件操作類:
PHP學(xué)習(xí)
<?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學(xué)習(xí)
<?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學(xué)習(xí)文件下載:
PHP學(xué)習(xí)
<?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學(xué)習(xí)更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP學(xué)習(xí)希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2243.html