有没有办法从php脚本重写现有的php文件

有没有办法从php脚本重写现有的php文件,php,html,zip,Php,Html,Zip,我从本机php构建了一个网站,我想让我的用户能够更改index.php,而无需手动编写代码,我的意思是: 我一直在构建某种“上传主题”系统,它允许我的用户上传zip文件。哪一个是包含的 js(文件夹) css(文件夹) 图像(文件夹) index.html 在上传和提取zip文件后,我需要将“index.html”更改为“index.php”,并将我的php脚本注入到index.php中 index.html ... 它将是index.php ... 是否可以通过单击php执行来实现

我从本机php构建了一个网站,我想让我的用户能够更改index.php,而无需手动编写代码,我的意思是:

我一直在构建某种“上传主题”系统,它允许我的用户上传zip文件。哪一个是包含的

  • js(文件夹)
  • css(文件夹)
  • 图像(文件夹)
  • index.html
在上传和提取zip文件后,我需要将“index.html”更改为“index.php”,并将我的php脚本注入到index.php中

index.html


...
它将是index.php


...

是否可以通过单击php执行来实现这一点?

基本上,这就是您可以做到的:

# Template file
$file       =   __DIR__.'/index.html';
# What you want to add
$prepend    =   '<?php include(\'/my/file.php\') ?>';
# Get contents of index.html
$raw        =   file_get_contents($file);
# Save the content back
file_put_contents($file,$prepend.PHP_EOL.$raw);
# Rename the file
rename($file,__DIR__.'/index.php');
#模板文件
$file=\uuuuu DIR\uuuu.'/index.html';
#你想补充什么
$prepend='';
#获取index.html的内容
$raw=file\u get\u contents($file);
#将内容保存回去
文件内容($file$prepend.PHP\u EOL.$raw);
#重命名文件
重命名($file,_DIR.'/index.php');

你可以很容易地结合使用
重命名()
文件内容()!。我是个傻瓜
file\u put\u contents()
是答案:D非常感谢。这很好地解释了我的解决方案!。您还可以将其设计为,当他们上载文件时,它会解压缩到
模板
文件夹中。您的
索引。php
会检查是否存在模板文件夹,并将其文件包含到默认索引页中。这样,你就不必对他们的文档内容进行任何修改。使用这种系统,网站可以随意切换模板(如果这是你计划做的,我不知道这是否相关)。我现在做的很简单,如果存在我上面提到的文件夹,我的脚本将在上传zip文件之前执行删除该文件夹。我是php的新手。因此,我将在未来的系统更新中遵循您的建议。
<?php include ('myscript.php') ?>
<!DOCTYPE html>
<html>...</html>
# Template file
$file       =   __DIR__.'/index.html';
# What you want to add
$prepend    =   '<?php include(\'/my/file.php\') ?>';
# Get contents of index.html
$raw        =   file_get_contents($file);
# Save the content back
file_put_contents($file,$prepend.PHP_EOL.$raw);
# Rename the file
rename($file,__DIR__.'/index.php');