从public_html文件夹内编辑public_html文件夹外的文件
问题描述:
使用php,如何编辑位于public_html文件夹内的文件位于public_html文件夹之外的文件夹中的文件?我试图在public_html文件夹外部使用包含文件文件名的include函数,但它运行外部文件来显示当前文件的内容(使用fputs)而不是仅更新外部文件。从public_html文件夹内编辑public_html文件夹外的文件
<?php
include "hits.txt";
$filename = "hits.txt";
$count = file($filename);
$count[0]++;
$file = fopen ($filename, "w") or die ("Cannot find $filename");
fputs($file, "$count[0]");
fclose($file);
?>
答
从我的理解,你想简单地编辑父目录中的文件?要做到这一点,您需要为文件提供一个绝对/相对路径的打开函数。即:
$file_relative = "../../file.txt";
$file_absolute = "/some_file/www/file.txt";
fopen($file_relative, "w");
// Edit your file as necessary
请注意,尽管这在技术上可行,但可能会被禁止。您可能没有适当的权限来编辑public_html以上的任何内容。
+0
谢谢。这正是我所期待的。 – programm3r 2012-07-24 15:08:42
显示您的代码。 – Crontab 2012-07-23 18:33:23
– programm3r 2012-07-23 20:31:22
我将您的代码添加到您的原始问题中。仅供参考,下次您可以编辑您的问题而不是评论。人们喜欢看到格式化的代码。欢迎来到SO。 :) – colonelclick 2012-07-23 21:47:42