1

我正在为模板编写一个简单的文本编辑器,并且我已经处理了打开、显示和编辑部分。每次我尝试保存它时,它都会在 fopen() 函数上给我一个错误。

我正在使用以下文件获取文件:

$dir = "./uploads/post-templates";
$files = scandir($dir);
while($files[0] == "." || $files[0] == "..") {
    array_shift($files);
}

然后一个简单的循环处理在选择菜单中显示文件名:

<?php foreach($files as $f) {   echo "<option name='file' value=" . $f . " class='file'>" . $f . "</option>";}; ?>

最后,使用一个简短的 jQuery 函数将其全部附加到 textarea 中。唉,当谈到执行脚本来保存文件时,我每次都会出错。我已经尝试对目录使用相对、绝对和 http,并且文件名和路径每次都正确回显。

///different file!!!!
$f = $_POST['file'];
$c = $_POST['content'];
$dir = "./uploads/post-templates/";
$file = $dir . $f;
echo $file;
$fo = fopen($file, "w") or die("opening error");
fwrite($fo, $c) or die("writing error");
fclose($f);
4

1 回答 1

0

注意:仅用于测试目的。

我写了一个测试脚本,它是成功的。

使用您拥有的 Mike 的值,尝试将我的脚本与您当前的传入值一起使用。

另外,这一行给了我原始代码中的一个错误:fclose($f);

错误:使用时fclose($f);

警告:fclose() 期望参数 1 是资源,字符串在...中给出

它应该读作fclose($fo);

测试代码:

<?php

$f = "thefile.txt";
$c = "the content";
$dir = "./test/";
$file = $dir . "/" . $f;
echo $file; // echos the file name at this point
$fo = fopen($file, "w") or die("opening error");
fwrite($fo, $c) or die("writing error");
fclose($fo);

// shows the contents of the written file on screen
$contents = file_get_contents($file);

echo $contents;

?>
于 2013-08-17T21:40:11.723 回答