我有一个索引.php文件。在我的根文件夹中,大约有 200 个文件夹。我想将索引.php文件添加到所有200文件夹中。除了复制粘贴,有没有一种快速添加它的方法?
有一种快速的方法可以做到这一点,但我觉得有更好的方法来做你需要的事情。
索引文件纯粹是为了阻止人们看到文件夹吗?如果是这样,请尝试将IndexIgnore *
放入您的.htaccess中
更多信息在这里: http://www.javascriptkit.com/howto/htaccess11.shtml
好吧,就像所有其他人已经说过的那样,您的设计可能有问题。如果你想确保人们无法访问它,只需使用Apache来做到这一点,而不是PHP。如果您仍然觉得需要将 index.php 文件添加到每个目录中,方法如下:
<?php
$content = 'Your content';
$files = glob( './*' );
foreach( $files as $file ) {
if( is_dir( $file ) && is_writable( $file ) ) {
file_put_contents( $file . '/index.php', $content );
}
}
使用此函数:
function putindexfiles($putfile,$start=""){
$files = glob($start.'*',GLOB_MARK);
foreach($files as $file){
if(is_dir($file)){
copy($putfile,$file.'index.php');
putindexfiles($putfile,$file);
}
}
}
此函数使用递归,因此所有子目录也会受到影响。
$putfile 是其内容要放置在每个文件夹的索引中的文件的路径.php。 此脚本在每个文件夹中自动创建所有索引.php
使用该函数时,您无需提供$start因为这是默认参数是放置脚本的目录。
享受