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/n/237065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:03
下一篇 2024-12-12 12:03

相关推荐

  • PHP和Python哪个好找工作?

    PHP和Python都是非常流行的编程语言,它们被广泛应用于不同领域的开发中。但是,在考虑择业方向的时候,很多人都会有一个问题:PHP和Python哪个好找工作?这篇文章将从多个方…

    编程 2025-04-29
  • vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常问题的解决

    本文旨在解决vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常的问题,提供完整的代码示例供参考。 一、分析问题 首先,需了解vue中下载文件的情况。一般情况下,我们…

    编程 2025-04-29
  • Python数据统计案例的实现

    Python作为一个高级编程语言,拥有着丰富的数据处理库和工具,能够快速、高效地进行各类数据处理和分析。本文将结合实例,从多个方面详细阐述Python数据统计的实现。 一、数据读取…

    编程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件并生成完整的图像

    OBJ格式是一种用于表示3D对象的标准格式,通常由一组顶点、面和纹理映射坐标组成。在本文中,我们将讨论如何将多个OBJ文件拼接在一起,生成一个完整的3D模型。 一、读取OBJ文件 …

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • 为什么用cmd运行Java时需要在文件内打开cmd为中心

    在Java开发中,我们经常会使用cmd在命令行窗口运行程序。然而,有时候我们会发现,在运行Java程序时,需要在文件内打开cmd为中心,这让很多开发者感到疑惑,那么,为什么会出现这…

    编程 2025-04-29
  • Python中读入csv文件数据的方法用法介绍

    csv是一种常见的数据格式,通常用于存储小型数据集。Python作为一种广泛流行的编程语言,内置了许多操作csv文件的库。本文将从多个方面详细介绍Python读入csv文件的方法。…

    编程 2025-04-29
  • Python将矩阵存为CSV文件

    CSV文件是一种通用的文件格式,在统计学和计算机科学中非常常见,一些数据分析工具如Microsoft Excel,Google Sheets等都支持读取CSV文件。Python内置…

    编程 2025-04-29
  • Python zipfile解压文件乱码处理

    本文主要介绍如何在Python中使用zipfile进行文件解压的处理,同时详细讨论在解压文件时可能出现的乱码问题的各种解决办法。 一、zipfile解压文件乱码问题的根本原因 在P…

    编程 2025-04-29
  • Python如何导入py文件

    Python是一种开源的高级编程语言,因其易学易用和强大的生态系统而备受青睐。Python的import语句可以帮助用户将一个模块中的代码导入到另一个模块中,从而实现代码的重用。本…

    编程 2025-04-29

发表回复

登录后才能评论