Home > php教程 > PHP源码 > 建立url或者路径path的文件夹

建立url或者路径path的文件夹

PHP中文网
Release: 2016-05-25 17:12:50
Original
1244 people have browsed it

php中文网

/***
	建立url或者路径path的文件夹,并将url里的文件夹设置为可写,
	如果不指定路径则在当前目录建立文件夹,否则在设置路径里建立文件夹。
	$url : 可为任意目录形式url,如'1/2/3/4/'、'1/2/3/4'、'http://www.5jiuai.com/1/2/3/43/4/'
	$path : 可为任意形式的path,如'd:/test'、'd:\test'
	返回 : 路径目录
	例子:
	$url = 'http://www.5jiuai.com/1/2/3/43/4/';
	$path =  'd:/test';
	echo create_folder($url,'d:/test');
*/		
function create_folder($url,$path=''){

	$path_str = !empty($path) ? $path : realpath(dirname(__FILE__));	 //当 $c_path==''时,取当前目录

	$folder_path_str  = preg_match('/\\\/i',$path_str) ?  preg_replace('/\\\/i','/',$path_str) : $path_str;	 //将路径里的'\'全部转为'/' 
	$folder_path_arr  = preg_match('/\//i',$folder_path_str) ? explode('/',$folder_path_str) : array();	 //将路径分割为数组


	$no_http_url = preg_match('/http:\/\//i',$url) ? preg_replace('/http:\/\//i','',$url) : $url;	//去除url中的'http://' 字符 
	$no_http_url = preg_match('/\\\/i',$no_http_url) ? preg_replace('/\\\/i','/',$no_http_url) : $no_http_url; //将url里的'\'全部转为'/' 
	$folder_url_arr  = preg_match('/\//i',$no_http_url) ? explode('/',$no_http_url) : array();	//将url分割为数组

	$path_arr = @array_merge($folder_path_arr,$folder_url_arr);	 //合并数组

	$path_way = '';
	foreach($path_arr as $key => $val){
		if(empty($val)) continue;		   //如果$val为空值,则跳转到下一步

		$path_way .= $val.'/';

		if(is_dir($path_way)) continue;	    //如果目录已经存在,则跳转到下一步

		@mkdir($path_way);
		if(in_array($val,$folder_url_arr)) @chmod($path_way,0777);	  //将url里的目录设置为可写
	}
	return 	$path_way;
}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template