php-文件存取

    更改文件的权限,组别及所有者

  •     chmod(f,mode)
  •     chgrp(f,group)
  •     chown(f,owner)

    复制文件

  •     copy(src,dst)

    返回目录的可用空间

  •     disk_free_space(dir)

    把整个文件读入一共数组中

  •     file(f)
  •     file(f,flag)

    检查文件或目录是否存在

  •     file_exists(f)

    将文件读取字符串

  •     file_get_contents(f)

    将字符串写入文件

  •     file_put_contents(f,text)
  •     file_put_contents(f,text,flag)

    返回一个包含匹配指定模式的文件名/目录的数组

  •     glob(pattern)

 返回文件的上次修改时间

  •     filemtime(f)

    返回文件的权限

  •     fileperms(f)

    返回文件大小

  •     filesize(f)

    判定我、指定的文件名是否是一个目录

  •     is_dir(f)

    判断文件是否可读/可写

  •     is_readable(f)
  •     is_writable(f)

    创建目录

  •     mkdir(dir)
  •     mkdir(dir,perms,recusive)

    重命名文件或目录

  •     rename(src,dst)

    删除空的目录

  •     rmdir(dir)

    返回目录中所有文件名

  •     scandir(dir)

    删除文件

  •     unlink(f)

 file_put_contents、file_get_contents、file实现文件的读写---翻转文字区别(一个是数组、一共是字符串)


<?php
	function reverse_line($filename){
		$text=file_get_contents($filename);//产生的是字符串
		echo "字符串:".$text;
		echo "<br>";
		$lines=explode("\n",$text);//将字符串以指定的分隔符分成数组
		print_r($lines);
		echo "<br>";
		$lines=array_reverse($lines);//翻转数组
		print_r($lines);
		echo "<br>";
		$text=implode("\n",$lines);//将数组转换成字符串
		echo $text;
		echo "<br>";
		file_put_contents($filename,$text);//将字符串写入数组
		echo "<hr>";
	}
	
	function reverse_lines($filename){
		$lines=file($filename);//读入的是数组
		print_r($lines);
		echo "<br>";
		$lines=array_reverse($lines);
		print_r($lines);
		echo "<br>";
		$text=implode("",$lines);//将数组转换成字符串
		echo $text;
		echo "<br>";
		file_put_contents($filename,$text);//将数组转换成字符串写入文件中
		echo "<hr>";
	}
	
	$a="test.txt";
	reverse_lines($a);
	//reverse_line($a);

?>

计算文件中空行的个数:(数组中开始和结尾的空格长度为0--一定是空行)

//trim移除字符串左右两侧的空格——如果长度为0就是空行


<?php
function count_blank_lines($filename){
	$count=0;//用来计数
	foreach(file($filename) as $line){//file($filename)读取文件内容保存在数组中
		//foreach遍历数组中的元素
		if(strlen(trim($line))==0){//trim移除字符串左右两侧的空格——如果长度为0就是空行
			$count++;
		}
	}
	return $count;
	
}
	
	$a="test.txt";
	echo count_blank_lines($a);
	

?>

    返回目录中所有文件名(实际上对于某一个目录都有两个特殊的文件.文件和..文件)

  •     scandir(dir)-注意文件的路径问题
<?php
	$a="测试";
	$files=scandir($a);
	foreach($files as $file){
		print "{$a}文件下的文件有:{$file}<br>";
	}
?>

通过条件过滤、过滤掉不需要的文件(加上判断)

返回一个包含匹配指定模式的文件名/目录的数组

  •     glob(pattern)---------该文件下面的jpg文件
<?php
	$a="测试";
	foreach(glob("$a\*.jpg") as $file){
		print "{$a}文件下的文件有:{$file}<br>";
	}
?>

 类和对象

<?php
	class name{
		private $name;//字段
		public function __onstruct(name){ //构造体(初始化新对象)
			statement;
		}public function name(paremeters){//方法(对象的行为)
			statement;
		}
	}
	//使用this应用类和方法
	$this->fieldName
	$this->methodName(paremeters);
?>

创建银行账户类”————注意使用的是两个下划线


<?php
	class bankaccount{//定义一个银行账户类
		private $name;//字段
		private $balance;//字段
		public function __construct($name){ //构造体(初始化新对象)
			$this->name=$name; 
			$this->balance=0.00;
		}
		public function getbalance(){//方法1_获取账户的金额
			return $this->balance;
		}
		public function getname(){//方法2——获取账户的姓名
			return $this->name;
		}
		public function deposit($amount){//方法3-存款进账户
			if($amount>=0){
				$this->balance+=$amount;
			}
		}
		public function withdraw($amount){//方法4-取款出账户 取的钱小于有的钱才可以存储
			if($amount>=0&&$amount<=$this->balance){
				$this->balance-=$amount;
			}
		}
		public function __toString(){//方法5-输出姓名—+金额
			return "{".$this->name.",$".$this->balance."}";
		}
	}
?>

使用new创建对象 


<!DOCTYPE html>
<html>
<head>
	<title>Document</title>
</head>

<body>
<?php
	include("exp1.php");
	$account1=new bankaccount("张三");//new 创建对象
	$account1->deposit(5);
	$account1->withdraw(2.3);
	$account1->withdraw(2.3);
	
	$account2=new bankaccount("李四");//new 创建对象
	$account2->deposit(50);
	$account2->deposit(-7);//存款只有大于0才有效
?>
<div>
<?=$account1?>
<?=$account2?>
</div>

</body>
</html>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值