码迷,mamicode.com
首页 > Web开发 > 详细

PHP Files functions

时间:2020-01-23 12:43:37      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:order   simple   get   fgets   put   reading   after   err   nts   

simple functions

<?php
$docRoot = $_SERVER['DOCUMENT_ROOT'];
//readfile($docRoot."/orders/order.txt"); //do not need fopen
unlink($docRoot."/orders/order.txt");
$fp = fopen($docRoot."/orders/orders.txt",rb);
if($fp){
    echo "open ok";
}else{
    echo 'error';
}
while (!feof($fp)){
    echo fgetc($fp);
}
//fpassthru($fp);  //out put string   needs fopen
//$len = fwrite($fp,'12345',3);
rewind($fp);
fseek();
fclose($fp);
//file_put_contents($docRoot/orders/order.txt,'123',FILE_APPEND);

Examples

<?php
  // create short variable name
  $document_root = $_SERVER['DOCUMENT_ROOT'];
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Bob's Auto Parts - Customer Orders</title>
  </head>
  <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Customer Orders</h2> 
    <?php
      @$fp = fopen("$document_root/orders/Book1.csv", 'rb');
      flock($fp, LOCK_SH); // lock file for reading

      if (!$fp) {
        echo "<p><strong>No orders pending.<br />
              Please try again later.</strong></p>";
        exit;
      }

      while (!feof($fp)) {
         $order= fgets($fp);
         echo htmlspecialchars($order)."<br />";
      }

      flock($fp, LOCK_UN); // release read lock

      echo 'Final position of the file pointer is '.(ftell($fp));
      echo '<br />';
      rewind($fp);
      echo 'After rewind, the position is '.(ftell($fp));
      echo '<br />';

//    while (!feof($fp)) {
//
//          $s= fgetcsv($fp,9,',');
//          echo "<pre>";
//     var_dump($s);
//    }

    if(is_null($s)) {
        echo 'null';
    }
      fclose($fp); 

    ?>
  </body>
</html>

PHP Files functions

标签:order   simple   get   fgets   put   reading   after   err   nts   

原文地址:https://www.cnblogs.com/luoxuw/p/12230428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!