PHP實(shí)現(xiàn)的文件操作類及文件下載功能示例
本文實(shí)例講述了PHP實(shí)現(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運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php下載遠(yuǎn)程大文件(獲取遠(yuǎn)程文件大小)的實(shí)例
- 淺談php fopen下載遠(yuǎn)程文件的函數(shù)
- 從性能方面考慮PHP下載遠(yuǎn)程文件的3種方法
- PHP下載遠(yuǎn)程文件到本地存儲(chǔ)的方法
- php帶密碼功能并下載遠(yuǎn)程文件保存本地指定目錄 修改加強(qiáng)版
- php下載遠(yuǎn)程文件類(支持?jǐn)帱c(diǎn)續(xù)傳)
- php實(shí)現(xiàn)的支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類
- PHP文件下載類
- 解決PHP超大文件下載,斷點(diǎn)續(xù)傳下載的方法詳解
- PHP實(shí)現(xiàn)的下載遠(yuǎn)程文件類定義與用法示例
相關(guān)文章
關(guān)于shopex同步ucenter的redirect問題,導(dǎo)致script不運(yùn)行
本文小編為大家介紹,關(guān)于shopex同步ucenter的redirect問題,導(dǎo)致script不運(yùn)行。有需要的朋友可以參考一下2013-04-04php生成隨機(jī)字符串可指定純數(shù)字、純字母或者混合的
這篇文章主要介紹了php生成隨機(jī)字符串的實(shí)現(xiàn)可指定純數(shù)字、純字母或者混合的2014-04-04php在多維數(shù)組中根據(jù)鍵名快速查詢其父鍵以及父鍵值的代碼
有一個(gè)多維數(shù)組,有多少維大家可以自定義。假如我們要在這個(gè)數(shù)組中找一個(gè)鍵為'subIndex'的值,我們可以用for、foreach等方法遍歷查找 反過來,假如我們?nèi)我饨o出一個(gè)或多個(gè)鍵,要求找出這個(gè)鍵的父級(jí)數(shù)組的鍵和值。這又如何實(shí)現(xiàn)?2011-05-05PHP+Mysql日期時(shí)間如何轉(zhuǎn)換(UNIX時(shí)間戳和格式化日期)
UNIX時(shí)間戳和格式化日期是我們常打交道的兩個(gè)時(shí)間表示形式,Unix時(shí)間戳存儲(chǔ)、處理方便,但是不直觀,格式化日期直觀,但是處理起來不如Unix時(shí)間戳那么自如,所以有的時(shí)候需要互相轉(zhuǎn)換,下面給出互相轉(zhuǎn)換的幾種轉(zhuǎn)換方式2012-07-07PHP 實(shí)現(xiàn) JSON 數(shù)據(jù)的編碼和解碼操作詳解
這篇文章主要介紹了PHP 實(shí)現(xiàn) JSON 數(shù)據(jù)的編碼和解碼操作,結(jié)合實(shí)例形式詳細(xì)分析了PHP操作json格式數(shù)據(jù)編碼、解碼函數(shù)使用場(chǎng)景及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-04-04