如何使用另一个php从php文件中删除行?
如果install.php不在同一目录中,我想从index.php顶部删除这6行。我真的不知道从哪里开始,怎么做如何使用另一个php从php文件中删除行?,php,directory,Php,Directory,如果install.php不在同一目录中,我想从index.php顶部删除这6行。我真的不知道从哪里开始,怎么做 <?php require ('config.php'); if (!defined('DATABASE_NAME')) { header('Location: install.php'); exit; } ?> 您可以创建条件流程,而不是删除: <?php if(file_exists('install.php')) {
<?php
require ('config.php');
if (!defined('DATABASE_NAME')) {
header('Location: install.php'); exit;
}
?>
您可以创建条件流程,而不是删除:
<?php
if(file_exists('install.php'))
{
require ('config.php');
if (!defined('DATABASE_NAME'))
{
header('Location: install.php'); exit;
}
}
?>
我不太清楚你为什么要删除它们 文件_exists()可以解决您的问题吗?比如:
if (file_exists("install.php")) {
// do something
} else {
// do something else
}
在物理删除中删除?或者删除“如果install.php未退出,请不要执行此代码”?试试我的建议。它工作得很好。我已经测试过了。问题是,我不希望index.php以这些行开头,每次都检查install.php是否存在。
<?php
//SplFileObject (Standard PHP Library) works at PHP 5.1.0+
$line_to_strip = 6; //be sure that the first 6 lines to be deleted, no empty lines.
$new_file = new SplFileObject('index.new', 'w');
foreach (new LimitIterator(new SplFileObject('index.php'), $line_to_strip) as $line)
$new_file->fwrite($line);
//rename the existing index.php file to index.old in order to feel safe
rename('index.php','index.old');
//or delete the old index.php file which I do not recommend
unlink('index.php');
//Since there is no close() function we have to set $new_file null
//Otherwise you get "file already open and cannot be changed" error
$new_file = null;
//rename index.new to index.php
rename('index.new','index.php');
?>
if (file_exists("install.php")) {
// do something
} else {
// do something else
}