php讀文件案例(php讀取文本文件)

本文目錄一覽:

怎麼用php讀取ppt文檔的內容,並且在瀏覽器

本文實例講述了PHP讀取PPT文件的方法。分享給大家供大家參考,具體如下:

最近做一個和FLASH有關的東西,其中就要用到在網站上看PPT就像百度,豆丁網那樣可以直接在網站上讀,在網上搜了半天沒搜到,都是些什麼安裝個軟體什麼的,PHP網站放到空間上,誰能讓你在哪裝軟體呢?不是在瞎扯么?不過還好,最後在國外一個網站上搜到了一個解決思路,就是一個PHP操作PPT的類,當然這個網站還提供了操作OFFICES軟體的其他類,不過是2007版的OFFICES

下面也貼出當時看到的一個例子:

本例子使用phppowerpoint類,使用該類注意兩點,一個是了解參數含義,二注意字符集

?

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242

?phpheader(“content-type:text/html;charset=utf-8″);//字體設置防止亂碼error_reporting(E_ALL);/** Include path **/set_include_path(get_include_path() . PATH_SEPARATOR . ‘../Classes/’);/** PHPPowerPoint */include ‘PHPPowerPoint.php’;/** PHPPowerPoint_IOFactory */include ‘PHPPowerPoint/IOFactory.php’;// Create new PHPPowerPoint object//echo date(‘H:i:s’) . ” Create new PHPPowerPoint object\n”;$objPHPPowerPoint = new PHPPowerPoint();$objPHPPowerPoint-getProperties()-setCreator(“Maarten Balliauw”);$objPHPPowerPoint-getProperties()-setLastModifiedBy(“Maarten Balliauw”);$objPHPPowerPoint-getProperties()-setTitle(“Office 2007 PPTX Test Document”);$objPHPPowerPoint-getProperties()-setSubject(“Office 2007 PPTX Test Document”);$objPHPPowerPoint-getProperties()-setDescription(“Test document for Office 2007 PPTX, generated using PHP classes.”);$objPHPPowerPoint-getProperties()-setKeywords(“office 2007 openxml php”);$objPHPPowerPoint-getProperties()-setCategory(“Test result file”);// Remove first slide//echo date(‘H:i:s’) . ” Remove first slide\n”;$objPHPPowerPoint-removeSlideByIndex(0);// Create templated slide//echo date(‘H:i:s’) . ” Create templated slide\n”;/*$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(200);$shape-setWidth(600);$shape-setOffsetX(10);$shape-setOffsetY(400);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘Introduction to’);$textRun-getFont()-setBold(true);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘PHPPowerPoint’);$textRun-getFont()-setBold(true);$textRun-getFont()-setSize(60);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create templated slideecho date(‘H:i:s’) . ” Create templated slide\n”;$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(100);$shape-setWidth(930);$shape-setOffsetX(10);$shape-setOffsetY(10);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘What is PHPPowerPoint?’);$textRun-getFont()-setBold(true);$textRun-getFont()-setSize(48);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(600);$shape-setWidth(930);$shape-setOffsetX(10);$shape-setOffsetY(100);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘- A class library’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘- Written in PHP’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘- Representing a presentation’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘- Supports writing to different file formats’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create templated slideecho date(‘H:i:s’) . ” Create templated slide\n”;$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(100);$shape-setWidth(930);$shape-setOffsetX(10);$shape-setOffsetY(10);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘What\’s the point?’);$textRun-getFont()-setBold(true);$textRun-getFont()-setSize(48);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(600);$shape-setWidth(930);$shape-setOffsetX(10);$shape-setOffsetY(100);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘- Generate slide decks’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ – Represent business data’);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ – Show a family slide show’);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ – …’);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘- Export these to different formats’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ – PowerPoint 2007’);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ – Serialized’);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ – … (more to come) …’);$textRun-getFont()-setSize(28);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create templated slideecho date(‘H:i:s’) . ” Create templated slide\n”;$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(100);$shape-setWidth(930);$shape-setOffsetX(10);$shape-setOffsetY(10);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘Need more info?’);$textRun-getFont()-setBold(true);$textRun-getFont()-setSize(48);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create a shape (text)echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(600);$shape-setWidth(930);$shape-setOffsetX(10);$shape-setOffsetY(100);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$textRun = $shape-createTextRun(‘Check the project site on CodePlex:’);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘ );$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘FFFFFFFF’ ) );// Create templated slideecho date(‘H:i:s’) . ” Create templated slide\n”;*///test//從資料庫調取數據進行for循環$row=array(‘titlepic’=array(‘./images/love.gif’,’./images/love1.gif’,’./images/love2.gif’,’./images/love3.gif’),’xsprice’=array(“55″,”33″,”22″,”333”),’cjid’=array(‘100′,’222′,’333′,’3333’),’lpid’=array(‘111′,’222′,’333′,’444’),’price’=array(‘111′,’433′,’243′,’3245’));for($i=0;$i4;$i++){$currentSlide = createTemplatedSlide1($objPHPPowerPoint,$row[“titlepic”][$i]); // local function// Create a shape (text)//echo date(‘H:i:s’) . ” Create a shape (rich text)\n”;$shape = $currentSlide-createRichTextShape();$shape-setHeight(100);$shape-setWidth(930);//調整字體的高度寬度$shape-setOffsetX(20);$shape-setOffsetY(400);//$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );$row[“price”]=iconv(“utf-8″,”gb2312”,$row[“price”][$i]);$textRun = $shape-createTextRun(‘禮品網價格:’.$row[“xsprice”][$i]);$textRun-getFont()-setBold(true);$textRun-getFont()-setSize(48);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘#000000’ ) );$shape = $currentSlide-createRichTextShape();$shape-setHeight(600);$shape-setWidth(930);$shape-setOffsetX(20);$shape-setOffsetY(500);$shape-getAlignment()-setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );//添加多行內容從這開始$textRun = $shape-createTextRun(‘公司編號: ‘.$row[“cjid”][$i]);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘#000000’ ) );$shape-createBreak();$textRun = $shape-createTextRun(‘禮品網編號: ‘.$row[“lpid”][$i]);$textRun-getFont()-setSize(36);$textRun-getFont()-setColor( new PHPPowerPoint_Style_Color( ‘#000000’ ) );//test// Save PowerPoint 2007 file}//echo date(‘H:i:s’) . ” Write to PowerPoint2007 format\n”;$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, ‘PowerPoint2007’);$objWriter-save(str_replace(‘.php’, ‘.pptx’,__FILE__));header(“Content-type:application/vnd.ms-powerpoint;”);header(“location:02presentation.pptx”);// Echo memory peak usage//echo date(‘H:i:s’) . ” Peak memory usage: ” . (memory_get_peak_usage(true) / 1024 / 1024) . ” MB\r\n”;// Echo done//echo date(‘H:i:s’) . ” Done writing file.\r\n”;/*** Creates a templated slide** @param PHPPowerPoint $objPHPPowerPoint* @return PHPPowerPoint_Slide*/function createTemplatedSlide1(PHPPowerPoint $objPHPPowerPoint,$cs1){// Create slide$slide = $objPHPPowerPoint-createSlide();// Add background image$shape = $slide-createDrawingShape();$shape-setName(‘Background’);$shape-setDescription(‘Background’);$shape-setPath(‘./images/realdolmen_bg.jpg’);$shape-setWidth(950);$shape-setHeight(720);$shape-setOffsetX(0);$shape-setOffsetY(0);// Add logo$shape = $slide-createDrawingShape();$shape-setName(‘PHPPowerPoint logo’);$shape-setDescription(‘PHPPowerPoint logo’);$shape-setPath($cs1);$shape-setHeight(120);$shape-setOffsetX(10);$shape-setOffsetY(10);// Return slidereturn $slide;}

php如何動態讀取一個文件內容?

你想無刷新顯示內容就用 ajax 實現吧,後台只負責返回這個文件的內容,前台一直不停的發送請求:

// Javascript

setInterval(function () {

    $.ajax(

        {

            url : url,         // 請求地址

            type : type,       // 請求方式

            dataType : ‘JSON’, // 返回數據的格式

            success : function (response) {

                console.log(response);  // 最終數據

            },

            error : function () {

                console.log(‘Request Fail’);

            }

        }

    );

}, 1000);

// php

$filename = ‘data.txt’;

$f = fopen($filename);

$content = fread($f, filesize($filename));

fclose($f);

echo $content;  // 具體數據格式參照前台獲取方式

用php讀取txt內容

首先fopen讀取TXT文件,獲取一個文件指針,然後fgets獲取一行,再fgets繼續讀取下一行

官方例子:

?php

$f = fopen (“fgetstest.php”, “r”);

$ln= 0;

while (! feof ($f)) {

    $line= fgets ($f);

    ++$ln;

    printf (“%2d: “, $ln);

    if ($line===FALSE) print (“FALSE\n”);

    else print ($line);

}

fclose ($f);

這個前提是你的$f這個文件指針不能關閉,如果你想在不同請求的情況下實現,那就要吧$f做全局存儲了,看看存session可否(我沒做過,不確定,你試試看)

如何使用PHP讀取文本文件內容

利用PHP讀取文本文件的內容,其實很簡單,我們只需要掌握函數「file_get_contents();」的使用就可以了。下面,小編將作詳細的介紹。

工具/原料

電腦一台

WAMP開發環境

方法/步驟

file_get_content()函數介紹。使用file_get_contents()獲取txt文件的內容,具體參數說明如下:

2

具體實例說明。從文本文件tst.txt中讀取裡面的內容並顯示在瀏覽器中,具體代碼和圖示如下:

?php

$file = ‘tst.txt’;

$content = file_get_contents($file); //讀取文件中的內容

echo $content;

?

php如何讀取文本指定的內容?

php讀取文件內容:

—–第一種方法—–fread()——–

?php

$file_path = “test.txt”;

if(file_exists($file_path)){

$fp = fopen($file_path,”r”);

$str = fread($fp,filesize($file_path));//指定讀取大小,這裡把整個文件內容讀取出來

echo $str = str_replace(“\r\n”,”br /”,$str);

}

?

——–第二種方法————

?php

$file_path = “test.txt”;

if(file_exists($file_path)){

$str = file_get_contents($file_path);//將整個文件內容讀入到一個字元串中

$str = str_replace(“\r\n”,”br /”,$str);

echo $str;

}

?

—–第三種方法————

?php

$file_path = “test.txt”;

if(file_exists($file_path)){

$fp = fopen($file_path,”r”);

$str = “”;

$buffer = 1024;//每次讀取 1024 位元組

while(!feof($fp)){//循環讀取,直至讀取完整個文件

$str .= fread($fp,$buffer);

}

$str = str_replace(“\r\n”,”br /”,$str);

echo $str;

}

?

——-第四種方法————–

?php

$file_path = “test.txt”;

if(file_exists($file_path)){

$file_arr = file($file_path);

for($i=0;$icount($file_arr);$i++){//逐行讀取文件內容

echo $file_arr[$i].”br /”;

}

/*

foreach($file_arr as $value){

echo $value.”br /”;

}*/

}

?

—-第五種方法——————–

?php

$file_path = “test.txt”;

if(file_exists($file_path)){

$fp = fopen($file_path,”r”);

$str =””;

while(!feof($fp)){

$str .= fgets($fp);//逐行讀取。如果fgets不寫length參數,默認是讀取1k。

}

$str = str_replace(“\r\n”,”br /”,$str);

echo $str;

}

?

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237065.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:03
下一篇 2024-12-12 12:03

相關推薦

發表回復

登錄後才能評論