php 文件操作1

23 篇文章 0 订阅
21 篇文章 0 订阅

Read:

********************************

1 传统步骤

fopen() 

file_exists()

fread()

2 fgets():读取一行 

3 fgetss(): 从图去的文本中去掉任何HTML PHP标记

4 fgetc(): 读取一个字符的内容,可用于读取二进制内容

5 file_get_contents: 可将整个文件读入到一个字符串,可以读取二进制文件。而且:不许要fopen(),NB!

6 file() : 类似file_get_contents不许要fopen就读入整个文件,区别是:将文件内容按行划分封装到一个数组中返回。

 

Write:

********************************

叮嘱:linux对文件的写权限控制非常严格,即使你随便建立一个文件,想要使用fwrite写入,也要保证对此文件你用用写权限。

笔者在root下建立的普通文件,用is_writeable(等价is_writable,好人性化啊,呵呵)就无法写,最后只好手动改动写入文件的操作权限为读写,方写入。

 

1 传统步骤:1)打开文件 fopen(),获得句柄 2)写入fwrite() ,可之前先进行is_writeable()判断

2 fwrite === fputs

3 和file_get_contents对应这里有:file_put_contents,同样不许要fopen()

4 需要注意的是,如果每次写入都从头开始,则fopen时方式选择w,w+. 如果需要从文件末尾添加,则需要使用a or a

 

删除:

 

*****************************

unlink(这个命令在shell下也可以执行)

文件删除不了,一定是权限的问题,关键是那个地方的权限,要去判定。(还没解决,mark下)

 

复制

****************************

copy($file1,$file2)

刚开始还是不行,按照doc的说明$file2可以复制前不存在,但是由于权限的问题,我这里只要先自己创建一个文件

,然后chmod o+w file2 然后才可以将$file1中的数据复制到$file2中。

 

重命名

************************

rename(1,2)

:-),既然上面不能删除,不能在复制时创建文件,这里也是权限问题,估计是apache的权限问题。有待下一步解决。

 

 

[唉,我把用到的两个目录,/var/www以及我的个人home下的目录全部设置为apache所有( 我这里是www-data,要看你apahce的用户是怎么配置的了,以上问题解决】

 

目录操作:

 

1 创建目录

mkdir

2 打开目录

opendir

3 列出目录所有文件

readdir

4 关闭目录

closedir

5 删除目录

rmdir

 

这上面设置ok后,这些操作就没有遇到什么阻力。

【中间遇到点挫折,马虎造成的,在vim中将函数前加了$,系统也没报错,检测了半天没检查出来,一定要小心】 

 

 

文件权限:

int fileperms() 

eg: echo substr(sprintf('%o',fileperms("php_folder")), -4);

 

 

附上过程中实验过的代码:

***********************

 

<?php

/*/* $filename="/home/mean/array.php";

if(file_exists($filename))

{

    $file=fopen($filename,"r");

    echo "fopen is ok!<br>";

    $str=fread($file,filesize($filename));

    print_r($str);

    fclose($file);

 

}

else

{

    echo "file isn't exist!";

}

 

 

 

$filename1="/home/mean/qiao.txt";

if(file_exists($filename1))

{

    $file1=fopen($filename1,"r");

    echo "<br>fgetss:<br>";

    echo fgetss($file1);

    //print_r($str1);

    fclose($file1);

    echo "fgetss is ok2!<br>";

 

}

else

{

    echo "open error22!";

}

$fn="/home/mean/qiao.txt";

$fh=fopen($fn,"r");

$ct=fgetc($fh);

print_r($ct);

fclose($fh);

 

//$fh2=fopen($fn,"r");

$ct2=file_get_contents($fn);

print_r($ct2);

 

$ct3=file($fn);

print "<br>file:<br>";

print_r($ct3);

 

//Write:

$fn3="/home/mean/yong.txt";

$ct1="to be or not to be!";

 

if (is_writable($fn3) )

{

    $handle=fopen($fn3,"a+");

    if(!$handle)

    {

        echo "不能打开文件$fn3";

        break;

    }

 

    if(fwrite($handle,$ct1)== FALSE)

    {

        echo "can not write into $fn3";

        break;

    }

 

    if(fwrite($handle,$ct1)== FALSE)

    {

        echo "error in 2 w";

        exit;

    }

 

    echo "<br>fwrite: ok!<br>";

    fclose($handle);

}

else

{

    echo "<br>$fn3 不可写";

}

/*

$ct2='why dont u leave me';

if(is_writable($fn3))

{

    if(file_put_contents($fn3,$ct2)=== FALSE)

    {

        echo " file_put_contents:error";

        exit;

    }

    else

    {

        echo "<br>";

        echo "write: success!";

    }

}

else

{

    echo "文件$fn3不可写";

}

 */

/*

// 删除文件

$file_del="/home/mean/qiao2.txt";

//chmod($file_del,0777);

if( unlink($file_del)===FALSE)

{

    echo "delete error!";

}

 

$file11="qiao.txt";

$file22="qiaoqiao.txt";

if(copy($file11,$file22)){

    echo "copy ok";}

else

{

    echo "copy no";

    echo "<br>";

}

 

 

$file33="qiao3.txt";

if(rename($file11,$file33))

{

    echo "rename ok";

}

else

{

    echo "rename :no";

}

 

//创建目录

if(is_dir("qiaoCreat"))

{

    rmdir("qiaoCreat");

    echo "rmdir ok";

}

else

{

    mkdir("qiaoCreat");

    echo "creat ok";

}

 

$opendirhandle=opendir("/var/www");

if($opendirhandle==false)

{

    echo "open dir error!";

}

else

{

    echo "<br>";

    echo "open success!";

}

 

 */

$folder="/var/www/php";

if(is_dir($folder))

{

 

    echo "open";

    if(false !==($openhandle=opendir($folder)))

    {

        echo "open is ok";

    }

    else

    {

        echo "cannot opendir";

    }

    while(false!==($file=readdir($openhandle))){

        echo "begin:<br>";

        echo "$file";

 

        echo "<br>";

    }

    closedir($openhandle);

}else

{

    echo "read files error";

}

 

// 文件权限look:即可查看文件,也可查看文件夹

$right= fileperms("php");

echo substr(sprintf('%o',$right),-4); //要看懂这句话的意思,首先通过sprintf将right用八进制表示,然后去倒数4位显示。

***********************

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值