本文章收藏了大量的php中文件操作函数如有文件打开,创建,删除,更变组,读取写文件,文件上传以及打开远程文件,把内容写入文件等实例.
- $fp=fopen("test.txt","r");
-
- $fp=fopen("test.txt","r+");
-
- $fp=fopen("test.txt","w");
-
- $fp=fopen("test.txt","w+");
-
- $fp=fopen("test.txt","a");
-
- $fp=fopen("test.txt","a+");
-
- $fp=fopen("test.txt","wb+");
-
- $fp=fopen("c: est est.txt","r");
-
- $fp=fopen("http://www.domain.com/","r");
-
- $fp=fopen("ftp://username:password@domain.com/test.txt","r");
-
-
-
-
-
-
- $fp=fopen("test.txt","r");
- $msg=fread($fp,filesize("test.txt"));
- print "$msg";
- fclose($fp);
-
-
-
- $fp=popen("test.txt","r");
- $fp=popen($_post['command'],'r');
- $read=fread($fp,2096);
- echo $read;
- pclose($fp);
-
-
- $fp=popen($_post['command'],'r');
- $read=fread($fp,2096);
- echo $read;
- pclose($fp);
-
-
-
- $file="test.txt";
- if(file_exists($file))
- {
- echo "下面清除缓存";
- }
- echo "<p>";
- clearstatcache();
- if(file_exists($file))
- {
- die('清除完毕');
- }
-
-
-
- $filename="test.txt";
- $user="admin";
- chgrp($filename,$group);
-
-
-
- chmod("/test/testfile.txt",0600);
-
- chmod("/test/testfile.txt",0644);
-
- chmod("/test/testfile.txt",0755);
-
- chmod("/test/testfile.txt",0750);
-
-
-
-
- $file="test.txt";
- delete($file);
-
以下代码实现文件上传功能,首先用move_uploaded_file函数上传文件,如果失败就用copy函数上传文件,上传到指定目录并修改目录属性.
使用此代码要有文件上传权限,还要定义$path上传路径,另外必须有上传内容,单独使用,无输出内容,代码如下:
- if(function_exists('move_uploaded_file') && move_uploaded_file($attachment,$path))
- {
- chmod($path,0666);
- $attachment=$path;
- }
- elseif(copy($attachment,$path))
- {
- chmod($path,0666);
- $attachment=$path;
- }
|