Per customer request I implemented uploads and downloads. This will allow easy plugin/files upload and installation... AND SO MUCH MORE!
This file upload/downlod functionality is intended as both help to you and a lesson in Eval() and EvalWrap() mastery. Keep this in mind if you review the code and learn how it works.
1 | Instantiating elWpAPIfud
This class extends the elWpAPIpro so it's instantiated in the same way. It's the top of the food chain as it combines both elWpAPI and elWpAPIpro.
<?php
// The same as elWpAPI or elWpAPIpro (it extends elWpAPIpro)
$wpapi = new elWpAPIfud($xmlrpc, $username, $password);
?>
2 | Basic Functions
These functions are the basic functionality of this new class. They provide access to vital paths in your WordPress blog.
<?php
$abspath = $wpapi->fud_absPath(); // Will return the realpath(ABSPATH) or FALSE
$uploads_base = $wpapi->fud_uploadsPath(); // Will return the base uploads folder
$uploads_path = $wpapi->fud_uploadsNowPath(); // Will return the uploads folder for the current month
$wpaths = $wpapi->fud_primaryPaths(); // Returns the most important WP paths in one array
var_dump($wpapi->fud_isDirectory($abspath)); // Check if path is directory
var_dump($wpapi->fud_isFile($abspath)); // Check if path is file
var_dump($wpapi->fud_realPath($abspath)); // Resolves any EXISTING path to its absolute value
var_dump($wpapi->fud_fileSize($filepath)); // Gets the size of a file
var_dump($wpapi->fud_fileTimes($filepath)); // Gets the times of a file as an array
?>
3 | Listing Files and Folders
To master the remote files you need to know them. And these functions will list them for you, so you can do what you please with them.
Non-Recursive Listing of Files
<?php
// This is how you list level 0 files & folders in Uploads
var_dump($wpapi->fud_listPaths($uploads_base));
// This is how you list level 0 files in Uploads
var_dump($wpapi->fud_listFiles($uploads_base));
// This is how you list level 0 files in Uploads
var_dump($wpapi->fud_listFolders($uploads_base));
?>
Recursive Listing of Files
Unlike the non-recursive counterparts, these functions go all the way. They list everything. Considering I have around 10k files in my WordPress folder, the transferred size is quite hefty. So I serialized() and base64-ed the result before sending it back, to ensure delivery as very large arrays fail in WordPress XMLRPC. It's all done in the background, you don't need to worry, but in case you look at the code, you know why I did it.
<?php
// This is how you list ALL files & folders in Uploads
var_dump($wpapi->fud_listPathsRecursive($uploads_base));
// This is how you list ALL files in Uploads
var_dump($wpapi->fud_listFilesRecursive($uploads_base));
// This is how you list ALL files in Uploads
var_dump($wpapi->fud_listFoldersRecursive($uploads_base));
?>
4 | Uploading Files
Uploading files is very easy. Just take a ZIP, push it to the server and you get its path in return. Any file works (images, pdfs, texts). ZIP is intentionally used here to continue in the next Chapter where we extract it remotely.
<?php
// 1st arg is the remote file name(only), 2nd is local path, 3rd optional remote path
// By default, it's uploaded in the upload/year/month folder
$path = $wpapi->fud_uploadFile('a.zip', '/local/path/to/a.zip');
// And with a thrid parameter you can specify the upload base path
$path = $wpapi->fud_uploadFile('a.zip', '/local/path/to/a.zip', $uploads_base);
?>
5 | Extracting Remote ZIPs
The files you just uploaded are just too easy to extract. See the code below and love it!
<?php
// This is how you extract it to a specified folder
$wpapi->fud_extractRemoteZip($path, $uploads_path);
// And this is how you extract it to the remote folder where it resides
$wpapi->fud_extractRemoteZipInPlace($path);
?>
6 | Downloading Files
Download files from your remote WordPress blog installation is also too easy. See the examples blow.
<?php
// This is how you retrieve the binary data of an EXISTING remote file
$binary_data = $wpapi->fud_downloadFile('/remote/path/to/a.png');
// This is how you retrieve and store the binary data of an EXISTING remote file
$local_path = $wpapi->fud_downloadFile('/remote/path/to/a.png', '/local/path/to/a.png');
// Second function returns the path of the new file, not the binary after completion
?>


