PHP實現(xiàn)的文件操作類及文件下載功能示例
本文實例講述了PHP實現(xiàn)的文件操作類及文件下載功能。分享給大家供大家參考,具體如下:
文件操作類:
<?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); ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《PHP網(wǎng)絡編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關(guān)文章
關(guān)于shopex同步ucenter的redirect問題,導致script不運行
本文小編為大家介紹,關(guān)于shopex同步ucenter的redirect問題,導致script不運行。有需要的朋友可以參考一下2013-04-04php生成隨機字符串可指定純數(shù)字、純字母或者混合的
這篇文章主要介紹了php生成隨機字符串的實現(xiàn)可指定純數(shù)字、純字母或者混合的2014-04-04php在多維數(shù)組中根據(jù)鍵名快速查詢其父鍵以及父鍵值的代碼
有一個多維數(shù)組,有多少維大家可以自定義。假如我們要在這個數(shù)組中找一個鍵為'subIndex'的值,我們可以用for、foreach等方法遍歷查找 反過來,假如我們?nèi)我饨o出一個或多個鍵,要求找出這個鍵的父級數(shù)組的鍵和值。這又如何實現(xiàn)?2011-05-05PHP+Mysql日期時間如何轉(zhuǎn)換(UNIX時間戳和格式化日期)
UNIX時間戳和格式化日期是我們常打交道的兩個時間表示形式,Unix時間戳存儲、處理方便,但是不直觀,格式化日期直觀,但是處理起來不如Unix時間戳那么自如,所以有的時候需要互相轉(zhuǎn)換,下面給出互相轉(zhuǎn)換的幾種轉(zhuǎn)換方式2012-07-07PHP 實現(xiàn) JSON 數(shù)據(jù)的編碼和解碼操作詳解
這篇文章主要介紹了PHP 實現(xiàn) JSON 數(shù)據(jù)的編碼和解碼操作,結(jié)合實例形式詳細分析了PHP操作json格式數(shù)據(jù)編碼、解碼函數(shù)使用場景及相關(guān)操作注意事項,需要的朋友可以參考下2020-04-04