简体   繁体   English

Wordpress - 创建 Json 文件

[英]Wordpress - Creating Json File

I am having a problem while creating a json file using a wordpress plugin created by me.我在使用我创建的 wordpress 插件创建 json 文件时遇到问题。 My code consists of reading data from my database, putting it in an array and than storing it as a json file.我的代码包括从我的数据库中读取数据,将其放入数组中,然后将其存储为 json 文件。

I've tried to change the path of my json file to my desktop folder and it worked.我尝试将我的 json 文件的路径更改为我的桌面文件夹并且成功了。 I have tried to create a file using pure php on the same directory.我试图在同一目录上使用纯 php 创建一个文件。 And it worked also.它也起作用了。

This is my code:这是我的代码:

$fp = fopen('C:\Users\Home\Desktop\results.json', 'w+');
fwrite($fp, json_encode($response[0]));
fclose($fp);

I think I have found the solution.我想我已经找到了解决方案。 It seems like WordPress has a problem with relative and absolute paths, so to avoid problems, it is better to use the WordPress function plugin_dir_path( FILE ), which will return the path to your plugin file(PHP file ) as a string and then concatenate it with the rest of your path and that's it.似乎 WordPress 的相对路径和绝对路径有问题,所以为了避免问题,最好使用 WordPress function plugin_dir_path( FILE ),它将以字符串形式返回插件文件(PHP 文件)的路径,然后连接它与你的路径的 rest 就是这样。 It works.有用。 and it's adaptative.它是自适应的。 (on Linux, it will return Linux path format and the same as windows) (在Linux上,会返回Linux路径格式,和windows一样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM