【问题标题】:Getting Error on fopen in PHP在 PHP 中获取 fopen 错误
【发布时间】:2013-08-18 08:07:25
【问题描述】:

我正在为模板编写一个简单的文本编辑器,并且我已经处理了打开、显示和编辑部分。但是,每次我尝试保存它时,它总是在 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 中。唉,在执行脚本来保存文件时,我每次都会出错。我已经尝试对目录使用 relatives、absolutes 和 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);

【问题讨论】:

  • 你忘了说你到底遇到了什么错误?
  • 没有具体的,它触发了 my or die()
  • 您所做的只是获取SELECT 中文件列表的名称吗?为什么要使用fopen()?我会使用一张表(个人),但你可以glob('/path/to/directory/*.tmpl') 并浏览一组文件名。
  • 您检查过您的权限吗?
  • 我认为@Fred 指出的路径问题可能是问题的很大一部分。您需要打印出fopen 看到的那些值,以便确保它有意义。

标签: php


【解决方案1】:

注意:仅用于测试目的。

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

使用您拥有的 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;

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多