[wp-hackers] creating a text file within WordPress system - the right way - referring to Otto's article

Haluk Karamete halukkaramete at gmail.com
Wed Jul 30 18:27:06 UTC 2014


Every once in a while, for whatever crazy reason happens to be, I need to
dig in and create a temporary-txt-file.  - for mostly debugging purposes
and it is temporary.

( I already got the database version taken care of for my needs but I would
love to explore and challenge myself the txt file implementation of it)

Now... creating a txt file on the server is easy enough,
but doing-it-right is pretty complicated.

I read Otto's article at
http://ottopress.com/2011/tutorial-using-the-wp_filesystem/  ( I had also
watched his video on this particular subject but a looong time ago )

Anyway, I downloaded his plugin code (which refers to from that article
above ) in which he clearly says, do-not-use-this-in-production.

There in the plug in code ( which I posted below), there is a section where
he explains step by step and I get it.

But that code is addressing the needs of an operation that is running
within the context of the admin dashboard - perhaps to take care of the
saving of some form post field data into a txt file. the article is all
about how to do this right,.

Well, in my case, I'm not firing the create text file from within the
admin.

I will be triggering the call ( such as blp_create_a_text_file($here) )
from a stand-alone php page like this

$file_path = $upload_dir['path']).'test.txt';

blp_setfiletext($file_path,'hello world');

One little correction though..  When I said stand-alone page, I lied. Cause
that stand-alone page got these 2 babies:

define('WP_USE_THEMES', false);
require( $wp_blog_header_path);

my goal is to wrap all that safety procedures referred in that article
nicely into my blp_setfiletext function.

Looking at this code below though, I could not figure this out. that's
because the code is in-bed with the wp_nonce_url and $form_fields stuff -
which as I said earlier is not applicable to the simple operation I'm
after.

Any ideas how to proceed?



// check to see if we are trying to save a file
if (isset($_POST['save'])) {
    // okay, let's see about getting credentials
    $url = wp_nonce_url('themes.php?page=otto');
    if (false === ($creds = request_filesystem_credentials($url, $method,
false, false, $form_fields) ) ) {

        // if we get here, then we don't have credentials yet,
        // but have just produced a form for the user to fill in,
        // so stop processing for now

        return true; // stop the normal page form from displaying
    }

    // now we have some credentials, try to get the wp_filesystem running
    if ( ! WP_Filesystem($creds) ) {
        // our credentials were no good, ask the user for them again
        request_filesystem_credentials($url, $method, true, false,
$form_fields);
        return true;
    }

    // get the upload directory and make a test.txt file
    $upload_dir = wp_upload_dir();
    $filename = trailingslashit($upload_dir['path']).'test.txt';

    // by this point, the $wp_filesystem global should be working, so let's
use it to create a file
    global $wp_filesystem;
    if ( ! $wp_filesystem->put_contents( $filename, 'Test file contents',
FS_CHMOD_FILE) ) {
        echo "error saving file!";
    }
}


More information about the wp-hackers mailing list