萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php入門教程一(對文件訪問實現)

php入門教程一(對文件訪問實現)

對文件訪問實現的源代碼。主要講到php教程操作文件以及xml文檔實例方法。

<?php
// 創建一個新的pdf文檔句柄
$pdf = cpdf_open(0);
// 打開一個文件
cpdf_open_file($pdf);
// 開始一個新頁面(a4)
cpdf_begin_page($pdf, 595, 842);
// 得到並使用字體對象
$arial = cpdf_findfont($pdf, "arial", "host", 1);
cpdf_setfont($pdf, $arial, 10);
// 輸出文字
cpdf_show_xy($pdf, "this is an exam of pdf documents, it is a good lib,",50, 750);
cpdf_show_xy($pdf, "if you like,please try yourself!", 50, 730);
// 結束一頁
cpdf_end_page($pdf);
// 關閉並保存文件
cpdf_close($pdf);

// 如果要直接輸出到客戶端的話,把下面的代碼加上

$buf = cpdf_get_buffer($pdf);

$len = strlen($buf);

header("content-type: application/pdf");

header("content-length: $len");

header("content-disposition: inline; filename=pie_php.pdf");

print $buf;

cpdf_delete($pdf);
?>

xml文件

<?php
$dom = new domdocument();
$dom->load("order.xsl");
$proc = new xsltprocessor;
$xsl = $proc->importstylesheet($dom);

$xml = new domdocument();
$xml->load('order.xml');
$string = $proc->transformtoxml($xml);
echo $string;
?>

order.sls

<?xml version="1.0" encoding='gb2312' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:output encoding='gb2312'/>

  <xsl:param name="column" select="'sku'"/>
<xsl:param name="order" select="'ascending'"/>
  <xsl:template match="/">
    <html>
      <body>
        <xsl:apply-templates select="order">
          <xsl:with-param name="sortcolumn" select="$column" />
          <xsl:with-param name="sortorder" select="$order" />
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="order">
    <xsl:param name="sortcolumn" />
    <xsl:param name="sortorder" />
    <table border="1">
      <tr>
        <th>訂單號</th>
        <th>id</th>
        <th>說明</th>
        <th>價格</th>
        <th>數量</th>
        <th>合計</th>
      </tr>
      <xsl:apply-templates select="item">        
      </xsl:apply-templates>
    </table>
  </xsl:template>
  <xsl:template match="item">
    <tr>
      <td><xsl:value-of select="../account" /></td>
      <td><xsl:value-of select="sku" /></td>
      <td><xsl:value-of select="description" /></td>
      <td><xsl:value-of select="priceper" /></td>
      <td><xsl:value-of select="quantity" /></td>
      <td><xsl:value-of select="subtotal" /></td>
    </tr>
  </xsl:template>    
</xsl:stylesheet>

order.xml文件

<?xml version="1.0" encoding='gb2312' ?>
<order>
  <account>9900234</account>
  <item id="1">
    <sku>1234</sku>
    <priceper>5.95</priceper>
    <quantity>100</quantity>
    <subtotal>595.00</subtotal>
    <description>www.111cn.net</description>
  </item>
  <item id="2">
    <sku>6234</sku>
    <priceper>22.00</priceper>
    <quantity>10</quantity>
    <subtotal>220.00</subtotal>
    <description>足球</description>
  </item>
  <item id="3">
    <sku>9982</sku>
    <priceper>2.50</priceper>
    <quantity>1000</quantity>
    <subtotal>2500.00</subtotal>
    <description>手機包</description>
  </item>
  <item id="4">
    <sku>3256</sku>
    <priceper>389.00</priceper>
    <quantity>1</quantity>
    <subtotal>389.00</subtotal>
    <description>手機</description>
  </item>
  <numberitems>www.111cn.net</numberitems>
  <total>3704.00</total>
  <orderdate>07/07/2002</orderdate>
  <ordernumber>8876</ordernumber>
</order>

 

 

copyright © 萬盛學電腦網 all rights reserved