您的位置:

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;

}

?