Using WP_Filesystem instead of direct file access functions

VIP Go platform specific

This document is for sites running on VIP Go.

Learn more

Overview #

↑ Top ↑

Should you even use the filesystem? #

One question that should be asked is if filesystem operations are actually needed. Often times these can be removed completely. For example a common pattern for WordPress plugins is to create a “cache” version of a file in the uploads directory. On WordPress.com VIP since that file write is actually being sent to many servers in 20+ locations around the world that operation is very slow in comparison to a local file server. In those cases we strongly recommend against doing any sort of caching that uses the filesystem. For this kind of caching it’s usually better to use WordPress transients or the wp_cache_* functions.

If you are, for example, creating custom css, saving it and adding a link to it, instead consider saving that css in a transient and outputting it inline as part of the page.

↑ Top ↑

What are the differences on WordPress.com VIP Go #

Our VIP Go environment is designed for performance at large scale. As such, it has some differences from what a regular single WordPress server setup would have. One of these is in regards to the filesystem. There are 2 main differences

Filesystem writes are only permitted in /tmp/ and in the uploads folder.
For security reasons, Filesystem writes are limited to two locations. The /tmp/ folder to be used when processing files and the /uploads/ folder. When using the /tmp/ folder, remember that you are on a multi-server setup. That means that multiple requests may go to different servers. The file you uploaded in one request will likely not be available in the next. As such always handle file operations in a single request. For more information consult Writing files on WordPress VIP Go.

The /uploads/ folder isn’t actually a folder, it’s a CDN
On a regular WordPress server, the /uploads/ folder is a regular folder on the same server as your WordPress code. On WordPress VIP Go, your uploads server is actually many many servers distributed around the world. As such functions that directly look at the filesystem will not work. This includes file_get_contents(), file_put_contents(), fopen() as well as all other functions listed here: http://php.net/manual/en/ref.filesystem.php
The VIP Go filesystem implements the WP_Filesystem class which is used as a wrapper around all filesystem operations to make them work over FTP, SSH, direct, as well as our CDN.

↑ Top ↑

Example #

Here is an example of how to use the WP_Filesystem:

global $wp_filesystem;
if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
    $creds = request_filesystem_credentials( site_url() );
    wp_filesystem($creds);
}
$wp_filesystem->put_contents( wp_get_upload_dir()['basedir'] . '/test.txt', 'this is a test file');

You can learn more about the WP_Filesystem on the codex.

Ready to get started?

Drop us a note.

No matter where you are in the planning process, we’re happy to help, and we’re actual humans here on the other side of the form. 👋 We’re here to discuss your challenges and plans, evaluate your existing resources or a potential partner, or even make some initial recommendations. And, of course, we’re here to help any time you’re in the market for some robust WordPress awesomeness.