当前位置:Gxlcms > PHP教程 > PHP两个文件操作

PHP两个文件操作

时间:2021-07-01 10:21:17 帮助过:20人阅读

1.获取指定目录下所有文件,包括子文件夹下文件,使用到递归

	function get_all_file($dir){		$files = scandir($dir);		foreach($files as $file){			if($file == '.' || $file == '..') continue;			if(is_file($dir.'/'.$file)){				$res[] = $file;				continue;			}			foreach(get_all_file($dir.'/'.$file) as $ff){				$res[] = $ff;			}		}		return $res;	}

2.获取一个文件相对于另一个文件的相对路径

	//得到$file_2相对$file_1的相对路径	function get_rela_path($file_1,$file_2){		$array_1 = explode('/',$file_1);		$array_2 = explode('/',$file_2);		$deep = count(array_intersect_assoc($array_1,$array_2));				if(count($array_1)-$deep-1 == 0){			$f[] = '.';		}else{			$f = array_fill(0,count($array_1)-$deep-1,'..');		}		$l = array_slice($array_2,$deep);		return implode('/',array_merge($f,$l));	}


====================================================

下面奉上curl的一个demo

curl主要是curl_setopt中curlopt_*的理解和活用

比较常用的也就是下面这几个了,查查手册,搞清楚吧

	function getUrl($url){				$ch = curl_init();		$data = array('name'=>'zhaozonglu','age'=>21);		curl_setopt($ch,CURLOPT_URL,$url);		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);		curl_setopt($ch,CURLOPT_FAILONERROR,true);		curl_setopt($ch,CURLOPT_POST,true);		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);		$out = curl_exec($ch);		$info = curl_getinfo($ch,CURLINFO_HTTP_CODE);		curl_close($ch);		return $info;			}



版权声明:本文为博主原创文章,未经博主允许不得转载。

人气教程排行