Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 628 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to a file from my WordPress plugin

#1
I've written a custom plugin for my WordPress site that relies on reading/writing from an xml data file within the plugin folder. When I test this standard PHP code for file reading/writing, it will let me create/write to files located at the wp-admin/ level, but not files within the plugin folder, although it can read from both.



$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);


I get the following debug error:

> Warning:
> file_put_contents(

[To see links please register here]

)
> [function.file-put-contents]: failed
> to open stream: HTTP wrapper does not
> support writeable connections in
> /Applications/MAMP/htdocs/wp_mysite/wp-content/plugins/my-plugin/my-plugin.php
> on line 53


I'm currently running this off a local MAMP server, but want a solution that will let me package and publish the plugin on any WordPress server. What is the right approach?

Thanks-
Reply

#2
Don't access it via HTTP if you want to write to the file. Access it directly instead for both reading and writing as it's much faster and the most direct method to access a file.

To get the base plugin directory path, use the `WP_PLUGIN_DIR` constant:

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

This will prevent you from making use of HTTP which should not be used at all because of performance reasons and because HTTP does not support writing. But foremost, as it's a file on the server you have access to, access it directly.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through