zoukankan      html  css  js  c++  java
  • php目录间拷贝文件方法

    项目中需要从不同文件夹中同步文件,自己写了个小方法,备注一下

    设计思路 

    1.检查拷贝文件夹是否存在。

    2.逐级检查子文件夹内文件

    3.检查各个文件夹内文件

    4.检查目标目录路径是否存在,不存在时创建同名文件夹

    5.拷贝到目标目录。

    function finddir($path = '') {
    	//切换当前路径(递归用)
    	chdir ( $path );
    	$base_path = getcwd ();
    	//判断路径是否存在
    	if (is_dir ( $base_path )) {
    		//打开目录句柄。
    		$dir = opendir ( $base_path );
    		while ( ($file = readdir ( $dir )) !== false ) {
    			//排除根目录路径('.','..')
    			if (! in_array ( $file, array (".", ".." ) )) {
    				//文件拷贝
    				if (is_file ( $file )) {
    					//拷贝目标目录
    					$aim_path = str_replace ( 'downfile', 'copyfile', $base_path );
    					//判断目录是否存在,如果否,创建目录。
    					if (! is_dir ( $aim_path )) {
    						mkdir ( $aim_path, 0777, TRUE );
    					}
    					//拷贝到指定目录
    					copy ( $file, $aim_path . '/' . $file );
    				}
    				//查找子文件夹
    				if (is_dir ( $file )) {
    					finddir ( $file );
    					chdir ( '../' );
    				}
    			}
    		}
    		//关闭目录句柄
    		closedir ( $dir );
    	}
    }


  • 相关阅读:
    Map
    input输入框文字提示IE兼容
    Linux下实现获取远程机器文件
    ssl_error_rx_record_too_long
    Linux下访问网站
    Linux安装Jdk1.7
    bootstrap左右圆角按钮-适配手机页面
    jQuery使用load方法加载其他文档内容
    Js操作DOM小练习_01
    BootstrapValidator验证表单用法
  • 原文地址:https://www.cnblogs.com/y0umer/p/3838867.html
Copyright © 2011-2022 走看看