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

发布时间:2022-11-11

本文目录一览:

  1. 怎么用php读取ppt文档的内容,并且在浏览器
  2. php如何动态读取一个文件内容?
  3. 用php读取txt内容
  4. 如何使用PHP读取文本文件内容
  5. php如何读取文本指定的内容?

怎么用php读取ppt文档的内容,并且在浏览器

本文实例讲述了PHP读取PPT文件的方法。分享给大家供大家参考,具体如下: 最近做一个和FLASH有关的东西,其中就要用到在网站上看PPT就像百度,豆丁网那样可以直接在网站上读,在网上搜了半天没搜到,都是些什么安装个软件什么的,PHP网站放到空间上,谁能让你在哪装软件呢?不是在瞎扯么?不过还好,最后在国外一个网站上搜到了一个解决思路,就是一个PHP操作PPT的类,当然这个网站还提供了操作OFFICES软件的其他类,不过是2007版的OFFICES 下面也贴出当时看到的一个例子: 本例子使用phppowerpoint类,使用该类注意两点,一个是了解参数含义,二注意字符集

<?php
header("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
$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
$objPHPPowerPoint->removeSlideByIndex(0);
// Create templated 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 slide
    return $slide;
}
//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; $i < 4; $i++) {
    $currentSlide = createTemplatedSlide1($objPHPPowerPoint, $row["titlepic"][$i]); // local function
    // Create a shape (text)
    $shape = $currentSlide->createRichTextShape();
    $shape->setHeight(100);
    $shape->setWidth(930);
    $shape->setOffsetX(20);
    $shape->setOffsetY(400);
    $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'));
}
// Save PowerPoint 2007 file
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx', __FILE__));
header("Content-type:application/vnd.ms-powerpoint;");
header("location:02presentation.pptx");

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, 'r');
$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 文件的内容,具体参数说明如下: 具体实例说明。从文本文件 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);
}
?>

第二种方法 - file_get_contents()

<?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;
}
?>

第四种方法 - file()

<?php
$file_path = "test.txt";
if (file_exists($file_path)) {
    $file_arr = file($file_path);
    for ($i = 0; $i < count($file_arr); $i++) { // 逐行读取文件内容
        echo $file_arr[$i] . "<br />";
    }
    /* 
    foreach ($file_arr as $value) {
        echo $value . "<br />";
    }
    */
}
?>

第五种方法 - fgets()

<?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;
}
?>