显示所有文件和文件夹 php文件及文件夹操作(创建、删除、移动、复制)

WBOY
Lepaskan: 2016-07-28 08:26:50
asal
1090 orang telah melayarinya

创建FileUtil.php文件,内容及调用方式如下:

 php文件及文件夹操作(创建、删除、移动、复制)

<span>php

</span><span>/*</span><span>*
 * 操纵文件类
 * 
 * 例子:
 * FileUtil::createDir('a/1/2/3');                    测试建立文件夹 建一个a/1/2/3文件夹
 * FileUtil::createFile('b/1/2/3');                    测试建立文件        在b/1/2/文件夹下面建一个3文件
 * FileUtil::createFile('b/1/2/3.exe');             测试建立文件        在b/1/2/文件夹下面建一个3.exe文件
 * FileUtil::copyDir('b','d/e');                    测试复制文件夹 建立一个d/e文件夹,把b文件夹下的内容复制进去
 * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 测试复制文件        建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去
 * FileUtil::moveDir('a/','b/c');                    测试移动文件夹 建立一个b/c文件夹,并把a文件夹下的内容移动进去,并删除a文件夹
 * FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 测试移动文件        建立一个b/d文件夹,并把b/1/2中的3.exe移动进去                   
 * FileUtil::unlinkFile('b/d/3.exe');             测试删除文件        删除b/d/3.exe文件
 * FileUtil::unlinkDir('d');                      测试删除文件夹 删除d文件夹
 </span><span>*/</span><span>class</span><span> FileUtil {

    </span><span>/*</span><span>*
     * 建立文件夹
     *
     * @param string $aimUrl
     * @return viod
     </span><span>*/</span><span>function</span> createDir(<span>$aimUrl</span><span>) {
        </span><span>$aimUrl</span> = <span>str_replace</span>('', '/', <span>$aimUrl</span><span>);
        </span><span>$aimDir</span> = ''<span>;
        </span><span>$arr</span> = <span>explode</span>('/', <span>$aimUrl</span><span>);
        </span><span>$result</span> = <span>true</span><span>;
        </span><span>foreach</span> (<span>$arr</span><span>as</span><span>$str</span><span>) {
            </span><span>$aimDir</span> .= <span>$str</span> . '/'<span>;
            </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) {
                </span><span>$result</span> = <span>mkdir</span>(<span>$aimDir</span><span>);
            }
        }
        </span><span>return</span><span>$result</span><span>;
    }

    </span><span>/*</span><span>*
     * 建立文件
     *
     * @param string $aimUrl 
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> createFile(<span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>false</span><span>) {
            </span><span>return</span><span>false</span><span>;
        } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>true</span><span>) {
            FileUtil </span>:: unlinkFile(<span>$aimUrl</span><span>);
        }
        </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>);
        FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        </span><span>touch</span>(<span>$aimUrl</span><span>);
        </span><span>return</span><span>true</span><span>;
    }

    </span><span>/*</span><span>*
     * 移动文件夹
     *
     * @param string $oldDir
     * @param string $aimDir
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> moveDir(<span>$oldDir</span>, <span>$aimDir</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>);
        </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span> . '/'<span>;
        </span><span>$oldDir</span> = <span>str_replace</span>('', '/', <span>$oldDir</span><span>);
        </span><span>$oldDir</span> = <span>substr</span>(<span>$oldDir</span>, -1) == '/' ? <span>$oldDir</span> : <span>$oldDir</span> . '/'<span>;
        </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) {
            FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        }
        @ </span><span>$dirHandle</span> = <span>opendir</span>(<span>$oldDir</span><span>);
        </span><span>if</span> (!<span>$dirHandle</span><span>) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>while</span> (<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) {
            </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) {
                </span><span>continue</span><span>;
            }
            </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span> . <span>$file</span><span>)) {
                FileUtil </span>:: moveFile(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            } </span><span>else</span><span> {
                FileUtil </span>:: moveDir(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            }
        }
        </span><span>closedir</span>(<span>$dirHandle</span><span>);
        </span><span>return</span><span>rmdir</span>(<span>$oldDir</span><span>);
    }

    </span><span>/*</span><span>*
     * 移动文件
     *
     * @param string $fileUrl
     * @param string $aimUrl
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> moveFile(<span>$fileUrl</span>, <span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>if</span> (!<span>file_exists</span>(<span>$fileUrl</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> = <span>false</span><span>) {
            </span><span>return</span><span>false</span><span>;
        } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> = <span>true</span><span>) {
            FileUtil </span>:: unlinkFile(<span>$aimUrl</span><span>);
        }
        </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>);
        FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        </span><span>rename</span>(<span>$fileUrl</span>, <span>$aimUrl</span><span>);
        </span><span>return</span><span>true</span><span>;
    }

    </span><span>/*</span><span>*
     * 删除文件夹
     *
     * @param string $aimDir
     * @return boolean
     </span><span>*/</span><span>function</span> unlinkDir(<span>$aimDir</span><span>) {
        </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>);
        </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span> . '/'<span>;
        </span><span>if</span> (!<span>is_dir</span>(<span>$aimDir</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>$dirHandle</span> = <span>opendir</span>(<span>$aimDir</span><span>);
        </span><span>while</span> (<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) {
            </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) {
                </span><span>continue</span><span>;
            }
            </span><span>if</span> (!<span>is_dir</span>(<span>$aimDir</span> . <span>$file</span><span>)) {
                FileUtil </span>:: unlinkFile(<span>$aimDir</span> . <span>$file</span><span>);
            } </span><span>else</span><span> {
                FileUtil </span>:: unlinkDir(<span>$aimDir</span> . <span>$file</span><span>);
            }
        }
        </span><span>closedir</span>(<span>$dirHandle</span><span>);
        </span><span>return</span><span>rmdir</span>(<span>$aimDir</span><span>);
    }

    </span><span>/*</span><span>*
     * 删除文件
     *
     * @param string $aimUrl
     * @return boolean
     </span><span>*/</span><span>function</span> unlinkFile(<span>$aimUrl</span><span>) {
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span><span>)) {
            </span><span>unlink</span>(<span>$aimUrl</span><span>);
            </span><span>return</span><span>true</span><span>;
        } </span><span>else</span><span> {
            </span><span>return</span><span>false</span><span>;
        }
    }

    </span><span>/*</span><span>*
     * 复制文件夹
     *
     * @param string $oldDir
     * @param string $aimDir
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> copyDir(<span>$oldDir</span>, <span>$aimDir</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>);
        </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span> . '/'<span>;
        </span><span>$oldDir</span> = <span>str_replace</span>('', '/', <span>$oldDir</span><span>);
        </span><span>$oldDir</span> = <span>substr</span>(<span>$oldDir</span>, -1) == '/' ? <span>$oldDir</span> : <span>$oldDir</span> . '/'<span>;
        </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) {
            FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        }
        </span><span>$dirHandle</span> = <span>opendir</span>(<span>$oldDir</span><span>);
        </span><span>while</span> (<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) {
            </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) {
                </span><span>continue</span><span>;
            }
            </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span> . <span>$file</span><span>)) {
                FileUtil </span>:: copyFile(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            } </span><span>else</span><span> {
                FileUtil </span>:: copyDir(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            }
        }
        </span><span>return</span><span>closedir</span>(<span>$dirHandle</span><span>);
    }

    </span><span>/*</span><span>*
     * 复制文件
     *
     * @param string $fileUrl
     * @param string $aimUrl
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> copyFile(<span>$fileUrl</span>, <span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>if</span> (!<span>file_exists</span>(<span>$fileUrl</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>false</span><span>) {
            </span><span>return</span><span>false</span><span>;
        } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>true</span><span>) {
            FileUtil </span>:: unlinkFile(<span>$aimUrl</span><span>);
        }
        </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>);
        FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        </span><span>copy</span>(<span>$fileUrl</span>, <span>$aimUrl</span><span>);
        </span><span>return</span><span>true</span><span>;
    }

}

</span>?>
Salin selepas log masuk

 php文件及文件夹操作(创建、删除、移动、复制)

另一种调用方式:

<span>$fu</span> = <span>new</span><span> FileUtil();
</span><span>$fu</span>->copyFile('a/1/2/3', 'a/1/2/4');
Salin selepas log masuk

  推荐一个自己业余时间开发的网盘搜索引擎,360盘搜(www.360panso.com)

以上就介绍了显示所有文件和文件夹 php文件及文件夹操作(创建、删除、移动、复制),包括了显示所有文件和文件夹方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan