'Upload file to an FTP server using WordPress Filesystem_API
Trying to upload a text file from WordPress website to a remote FTP with standard WordPress Filesystem_API. The connection works fine, but could not upload files to the destination. The PHP standard ftp_put() works fine. File upload is successful using the FileZilla client. It's a plain FTP ( Not TLS, Not SSL )
Tried WordPress Developer guide - for ftpext, ftpsockets
function remote_ftp_upload_using_standard_wp($ftp_profile) {
global $wp_filesystem;
require_once ( ABSPATH . '/wp-admin/includes/file.php' );
$connection_method = 'ftpext';
$connection_options = array(
'connection_type' => $connection_method,
'hostname' => $ftp_profile['server'],
'username' => $ftp_profile['user_name'],
'password' => $ftp_profile['password'],
'port' => $ftp_profile['port'],
);
WP_Filesystem($connection_options);
if (!class_exists("WP_Filesystem_$connection_method")) {
$abstraction_file = ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $connection_method . '.php';
if (!file_exists($abstraction_file)) {
return;
}
require_once $abstraction_file;
}
$method = "WP_Filesystem_$connection_method";
$wp_filesystem = new $method($connection_options);
$wp_filesystem->connect(); // RETURNS TRUE
//var_dump($wp_filesystem->abspath());exit;
//$wp_filesystem->touch('/test.txt');
$wp_filesystem->put_contents('/test.txt', "hello"); // RETURNS FALSE
//ftp_pasv( $wp_filesystem->link, false );
return $wp_filesystem;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
