创建指定的文件:
1 //创建指定的文件
2 private function createfile(){
3 $savepath = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\";//文件物理路径
4 $filename = $savepath."t123.txt";
5 $fp = fopen("$filename","w+");//打开文件指针.
6 fwrite($fp,"This is my file for mcqueen.");//向新建的文件写入内容.
7 if(!is_writable($filename)){
8 die("文件:".$filename."不可写.请检查!");
9 }
10 fclose($fp);//关闭文件指针.
2 private function createfile(){
3 $savepath = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\";//文件物理路径
4 $filename = $savepath."t123.txt";
5 $fp = fopen("$filename","w+");//打开文件指针.
6 fwrite($fp,"This is my file for mcqueen.");//向新建的文件写入内容.
7 if(!is_writable($filename)){
8 die("文件:".$filename."不可写.请检查!");
9 }
10 fclose($fp);//关闭文件指针.
11 }
读取指定文件:
1 //读取指定的文件.
2 private function readfile(){
3 $filename = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\t123.txt";//文件物理路径
4 if( !file_exists($filename) ){
5 die("文件不存在");
6 }
7 //$data = file_get_contents($filename);//PHP5以后支持.
8 $data = implode('',file($filename));//可以读取二进制文件
9 echo $data;
10 }
2 private function readfile(){
3 $filename = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\t123.txt";//文件物理路径
4 if( !file_exists($filename) ){
5 die("文件不存在");
6 }
7 //$data = file_get_contents($filename);//PHP5以后支持.
8 $data = implode('',file($filename));//可以读取二进制文件
9 echo $data;
10 }
向指定文件内写入内容.:
1 //向指定文件写入内容.
2 private function wirtefile(){
3 $filename = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\t123.txt";//文件物理路径
4 //文件写入模式.
5 //w.可写;r.读;a.追加写入
6 $wtype = array(0=>"w", 1=>"r", 2=>"a");
7 if(!is_writable($filename)){
8 die("无法写入文件.不存在或权限不够.");
9 }
10 $data = "你永不清楚.你那天经过.留下万千追忆封锁我.";
11 //PHP5以后支持的写入文件函数.
12 //file_put_contents($filename,$data);
13 //PHP5以前.传统写入文件方法.
14 $f = fopen($filename,'a');
15 fwrite($f,$data);
16 fclose($filename);
2 private function wirtefile(){
3 $filename = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\t123.txt";//文件物理路径
4 //文件写入模式.
5 //w.可写;r.读;a.追加写入
6 $wtype = array(0=>"w", 1=>"r", 2=>"a");
7 if(!is_writable($filename)){
8 die("无法写入文件.不存在或权限不够.");
9 }
10 $data = "你永不清楚.你那天经过.留下万千追忆封锁我.";
11 //PHP5以后支持的写入文件函数.
12 //file_put_contents($filename,$data);
13 //PHP5以前.传统写入文件方法.
14 $f = fopen($filename,'a');
15 fwrite($f,$data);
16 fclose($filename);
17 }
读取目录下所有文件:
//读取目录下的所有文件
private function readdirfiles(){
$base_dir = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\";
$fso = opendir($base_dir);
while($flist = readdir($fso)){
echo $flist."<br />";
}
closedir($fso);
private function readdirfiles(){
$base_dir = "E:\Working Sites\PhpDemo\InitPHPFrame\demo\app\web\controller\demo\";
$fso = opendir($base_dir);
while($flist = readdir($fso)){
echo $flist."<br />";
}
closedir($fso);
}