Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP > php学习笔记5—(文件操作)
【标  题】:php学习笔记5—(文件操作)
【关键字】:php
【来  源】:http://blog.csdn.net/yxf/archive/2006/10/04/1320759.aspx

php学习笔记5—(文件操作)

Your Ad Here 文件操作基本上跟C是一样的。

<?php
    
# 获取服务器的/
    $doc_root=$_SERVER['DOCUMENT_ROOT'];
    
echo "$doc_root<br>";
    
$file1="$doc_root/file1.txt";
    
    
# 文件操作和C基本相同
    # open for write
    $fp=@fopen($file1,"wb"); # binary mode for default and more compatible
    if ( !$fp )  # if it failed, php will cause a warning
    {
        
echo "#1: cannot open file1.txt<br>";
        
exit;
    }
    
flock($fp,LOCK_EX); // LOCK_EX : write lock
                        // LOCK_SH : read lock
                        // LOCK_UN : release the lock
                        // LOCK_NB : 防止在请求加锁时发生阻塞

    fwrite($fp,"fwrite ");
    
fputs($fp,"fputs ");
    
flock($fp,LOCK_UN); // release write lock
    fclose($fp);
    
    
# open for read
    $fp=@fopen($file1,"rb");
    
if ( !$fp ) {
        
echo "#2: cannot open file1.txt<br>";
        
exit;
    }
    
while (!feof($fp)) {
        
# $str=fgetcsv($fp,100," "); <-- this returns an array seperated by delimiter
        $str=fgets($fp,1024); # fgets returns a line in the file, 
                              # 1024 is the length, strlen($str)<1024
        echo "$str<br>";
    }
    
fclose($fp);
    
    
# readfile() writes the content of the file into stdout
    echo "<pre>";
    
readfile($file1);
    
echo "</pre>";
    
    
# fpassthru() writes the content of the file into stdout and then close the file
    echo "<pre>";
    
$fp=fopen($file1,"rb");
    
fpassthru($fp);
    
echo "</pre>";
    
    
# fgetc() 
    $fp=@fopen($file1,"rb");
    
if ( !$fp ) {
        
echo "#3: cannot open file1.txt<br>";
        
exit;
    }
    
while (!feof($fp)) {
        
$c=fgetc($fp);
        
echo "$c<br>";
    }
    
fclose($fp);
    
    
# fread() 
    # file_exists()
    # filesize()
    # unlink() <-- delete a file
    if ( !file_exists($file1) ) {
        
echo '#4: '.$file1.' not found.<br>';
        
exit;
    }
    
$size=filesize($file1);
    
$fp=fopen($file1,'rb');
    
$str=fread($fp,$size);
    
echo "<pre>$str</pre>";
    
fclose($fp);
    
unlink($file1);    // delete file1.txt
    if ( !file_exists($file1) ) {
        
echo '#5: '.$file1.' not found.<br>';
        
exit;
    }
?>
几组汇编指令的比较:【上一篇】
windows+apache环境下运行C语言编写的CGI:【下一篇】
【相关文章】
  • php学习笔记1—(a+b)
  • php学习笔记2—(字符串和注释)
  • php学习笔记3—(变量、常量和操作符)
  • php学习笔记4—(控制结构)
  • PHP5乱记(3)还是权限问题,mysql启动了
  • php中foreach()的用法
  • PHP5中的this,self和parent关键字详解
  • 转: Linux环境下如何使用PHP,C,Perl处理图像
  • linux下配置APACHE2.0.50+PHP5.0.3+MYSQL4.0.20+GD库
  • Windows平台下PHP5.0+Mysql4.1.x环境架设(2)
  • 【随机文章】
  • 了解笔记本电脑的各类接口
  • hacmp的定义和发展
  • 正则表达式之道
  • 编程修养(3)
  • 电子杂志(商刊)迎来在线阅读新时代(无须任何插件或客户端)
  • SQL语言复习笔记2(INFORMIX)
  • 将XML字典所有单词的首二位取出并写入文件
  • 网工路由交换相关配置
  • hibernate开发日记
  • 在VB6或ASP中调用webservice
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.