Php 从我的WordPress插件写入文件

Php 从我的WordPress插件写入文件,php,file-io,wordpress,Php,File Io,Wordpress,我已经为我的WordPress站点编写了一个自定义插件,它依赖于从插件文件夹中的xml数据文件读/写。当我测试此标准PHP代码的文件读/写时,它将允许我创建/写入位于wp admin/级别的文件,但不包括插件文件夹中的文件,尽管它可以从这两个文件夹中读取 $file = 'test.xml'; (Can write to this file) $file = plugins_url()."/my-plugin/test.xml"; (Can read but not write to thi

我已经为我的WordPress站点编写了一个自定义插件,它依赖于从插件文件夹中的xml数据文件读/写。当我测试此标准PHP代码的文件读/写时,它将允许我创建/写入位于wp admin/级别的文件,但不包括插件文件夹中的文件,尽管它可以从这两个文件夹中读取

$file = 'test.xml';  (Can write to this file)
$file = plugins_url()."/my-plugin/test.xml";  (Can read but not write to this file)
// Open the file to get existing content
$current = file_get_contents($file);
echo $current;
// Append a new person to the file
$current .= "<person>John Smith</person>\n";
// Write the contents back to the file
file_put_contents($file, $current);
$file='test.xml';(可以写入此文件)
$file=plugins\u url().“/myplugin/test.xml”;(可以读取但不能写入此文件)
//打开文件以获取现有内容
$current=file\u get\u contents($file);
回声$电流;
//将一个新的人附加到文件中
$current.=“John Smith\n”;
//将内容写回文件
文件内容($file,$current);
我收到以下调试错误:

警告: 文件内容(http://localhost/wp_mysite/wp-content/plugins/my-plugin/test.xml) [function.file put contents]:失败 要打开流:HTTP包装器不支持 支持中的可写连接 /Applications/MAMP/htdocs/wp_mysite/wp content/plugins/my plugin/my-plugin.php 在线53

我目前正在本地MAMP服务器上运行此插件,但希望有一个解决方案可以让我在任何WordPress服务器上打包和发布插件。什么是正确的方法


谢谢-

如果要写入文件,请不要通过HTTP访问它。直接访问它,而不是读写,因为它的速度快得多,是访问文件最直接的方法

要获取基本插件目录路径,请使用
WP\u plugin\u DIR
常量:

$file = 'test.xml';  // (Can write to this file)
$file = WP_PLUGIN_DIR."/my-plugin/test.xml"; 
//      ^^^^^^^^^^^^^

这将阻止您使用HTTP,因为性能原因和HTTP不支持写入,所以根本不应该使用HTTP。但最重要的是,由于它是您有权访问的服务器上的一个文件,请直接访问它。

@Haker您知道主题是否存在类似的东西吗?类似于
WP\u THEME\u DIR
@Blowsie:不,不是这样一个常量,而是类似的东西。请参见此处了解“大局”: