Skip to content

Instantly share code, notes, and snippets.

@SirLouen
Created March 28, 2025 23:17
Show Gist options
  • Save SirLouen/ac3de9972a6b4c6d904f014921ab1a44 to your computer and use it in GitHub Desktop.
Save SirLouen/ac3de9972a6b4c6d904f014921ab1a44 to your computer and use it in GitHub Desktop.
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
require_once '../wp-load.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
function debug_ftp_connection() {
global $wp_filesystem;
echo '<h2>WordPress Filesystem Configuration</h2>';
echo '<p>FS_METHOD constant: ' . ( defined( 'FS_METHOD' ) ? FS_METHOD : 'Not defined' ) . '</p>';
echo '<p>FTP_BASE constant: ' . ( defined( 'FTP_BASE' ) ? FTP_BASE : 'Not defined' ) . '</p>';
echo '<p>FTP_CONTENT_DIR constant: ' . ( defined( 'FTP_CONTENT_DIR' ) ? FTP_CONTENT_DIR : 'Not defined' ) . '</p>';
echo '<p>FTP_PLUGIN_DIR constant: ' . ( defined( 'FTP_PLUGIN_DIR' ) ? FTP_PLUGIN_DIR : 'Not defined' ) . '</p>';
echo '<p>FTP_HOST constant: ' . ( defined( 'FTP_HOST' ) ? FTP_HOST : 'Not defined' ) . '</p>';
echo '<p>FTP_USER constant: ' . ( defined( 'FTP_USER' ) ? FTP_USER : 'Not defined' ) . '</p>';
if ( ! isset( $wp_filesystem ) ) {
echo '<p>Attempting to initialize filesystem...</p>';
$ftp_options = array(
'hostname' => defined( 'FTP_HOST' ) ? FTP_HOST : '',
'username' => defined( 'FTP_USER' ) ? FTP_USER : '',
'password' => defined( 'FTP_PASS' ) ? FTP_PASS : '',
'port' => 21,
'ssl' => defined( 'FTP_SSL' ) ? FTP_SSL : false,
'timeout' => 30,
);
echo '<p>FTP Options being passed:</p>';
echo '<pre>';
print_r( $ftp_options );
echo '</pre>';
if ( ! WP_Filesystem( $ftp_options ) ) {
echo '<p>Failed to initialize filesystem. Error: ' . $wp_filesystem->errors->get_error_message() . '</p>';
} else {
echo '<p>Successfully initialized filesystem</p>';
}
}
echo '<h2>FTP Connection Debug Information</h2>';
if ( isset( $wp_filesystem ) && is_object( $wp_filesystem ) ) {
echo '<p>Filesystem Method: ' . get_class( $wp_filesystem ) . '</p>';
echo '<h3>Basic Filesystem Tests:</h3>';
try {
$cwd = $wp_filesystem->cwd();
echo '<p>Current Working Directory: ' . ( $cwd ? $cwd : 'Could not determine' ) . '</p>';
echo '<p>Attempting to list root directory contents:</p>';
$root_files = $wp_filesystem->dirlist( '/' );
if ( is_array( $root_files ) ) {
echo '<pre>';
print_r( $root_files['wp-includes'] );
echo '</pre>';
} else {
echo '<p>Could not list root directory contents</p>';
}
echo '<p>Attempting to list src directory contents:</p>';
$includes_files = $wp_filesystem->dirlist( 'wp-includes' );
if ( is_array( $includes_files ) ) {
echo '<pre>';
print_r( $includes_files['version.php'] );
echo '</pre>';
} else {
echo '<p>Could not list src directory contents</p>';
}
echo '<p>Attempting to list wp-admin directory contents:</p>';
$wp_admin_files = $wp_filesystem->dirlist( 'src/wp-admin' );
if ( is_array( $wp_admin_files ) ) {
echo '<pre>';
print_r( $wp_admin_files );
echo '</pre>';
} else {
echo '<p>Could not list wp-admin directory contents</p>';
}
} catch ( Exception $e ) {
echo '<p>Error with directory operations: ' . $e->getMessage() . '</p>';
}
$test_dirs = array(
'wp-admin',
'wp-admin/includes',
'wp-content',
'wp-content/plugins',
);
echo '<h3>Directory Tests (with relative paths):</h3>';
foreach ( $test_dirs as $dir ) {
echo '<p>Testing directory: ' . $dir . '</p>';
echo '<ul>';
try {
echo '<li>Exists: ' . ( $wp_filesystem->exists( $dir ) ? 'Yes' : 'No' ) . '</li>';
echo '<li>Is Directory: ' . ( $wp_filesystem->is_dir( $dir ) ? 'Yes' : 'No' ) . '</li>';
echo '<li>Is Writable: ' . ( $wp_filesystem->is_writable( $dir ) ? 'Yes' : 'No' ) . '</li>';
$files = $wp_filesystem->dirlist( $dir );
echo '<li>Directory Contents: ' . ( is_array( $files ) ? count( $files ) . ' items' : 'Could not list contents' ) . '</li>';
} catch ( Exception $e ) {
echo '<li>Error testing directory: ' . $e->getMessage() . '</li>';
}
echo '</ul>';
}
// Test with very long paths.
echo '<h3>File Write Test:</h3>';
$test_file = 'wp-content/upgrades/wordpress-abcdefghijklmnopqrstuvwxyz/wordpress/wp-includes/test-write.txt';
try {
echo '<p>Attempting to write to: ' . $test_file . '</p>';
$dir = dirname( $test_file );
echo '<p>Directory exists: ' . ( $wp_filesystem->exists( $dir ) ? 'Yes' : 'No' ) . '</p>';
echo '<p>Directory is writable: ' . ( $wp_filesystem->is_writable( $dir ) ? 'Yes' : 'No' ) . '</p>';
if ( ! $wp_filesystem->exists( $dir ) ) {
echo '<p>Attempting to create directory: ' . $dir . '</p>';
$dir_parts = explode( '/', $dir );
$current_path = '';
foreach ( $dir_parts as $part ) {
if ( empty( $part ) ) {
continue;
}
$current_path .= ( empty( $current_path ) ? '' : '/' ) . $part;
echo '<p>Attempting to create directory: ' . $current_path . '</p>';
if ( $wp_filesystem->exists( $current_path ) ) {
echo '<p>Directory already exists: ' . $current_path . '</p>';
continue;
}
$mkdir_result = $wp_filesystem->mkdir( $current_path, 0755 );
echo '<p>Create directory result for ' . $current_path . ': ' . ( $mkdir_result ? 'Success' : 'Failed' ) . '</p>';
if ( ! $mkdir_result ) {
if ( isset( $wp_filesystem->link ) ) {
echo '<p>FTP Connection Status: Connected</p>';
echo '<p>FTP System Type: ' . ftp_systype( $wp_filesystem->link ) . '</p>';
echo '<p>FTP Features: ' . implode( ', ', ftp_raw( $wp_filesystem->link, 'FEAT' ) ) . '</p>';
echo '<p>Current Working Directory: ' . $wp_filesystem->cwd() . '</p>';
$current_files = $wp_filesystem->dirlist( dirname( $current_path ) );
echo '<p>Contents of parent directory:</p>';
echo '<pre>';
print_r( $current_files );
echo '</pre>';
} else {
echo '<p>FTP Connection Status: Not Connected</p>';
}
if ( isset( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
echo '<p>Filesystem Errors:</p>';
echo '<pre>';
print_r( $wp_filesystem->errors->get_error_messages() );
echo '</pre>';
}
break;
}
}
}
$write_result = $wp_filesystem->put_contents( $test_file, 'test' );
echo '<p>Write Test: ' . ( $write_result ? 'Success' : 'Failed' ) . '</p>';
echo '<p> File Exists: ' . ( $wp_filesystem->exists( $test_file ) ? 'Yes' : 'No' ) . '</p>';
if ( ! $write_result ) {
echo '<p>Write Error Details:</p>';
if ( isset( $wp_filesystem->errors ) ) {
echo '<pre>';
print_r( $wp_filesystem->errors->get_error_messages() );
echo '</pre>';
}
if ( isset( $wp_filesystem->link ) ) {
echo '<p>FTP Connection Status: Connected</p>';
echo '<p>FTP System Type: ' . ftp_systype( $wp_filesystem->link ) . '</p>';
echo '<p>FTP Features: ' . implode( ', ', ftp_raw( $wp_filesystem->link, 'FEAT' ) ) . '</p>';
echo '<p>Current Working Directory: ' . $wp_filesystem->cwd() . '</p>';
$target_files = $wp_filesystem->dirlist( dirname( $test_file ) );
echo '<p>Contents of target directory:</p>';
echo '<pre>';
print_r( $target_files );
echo '</pre>';
} else {
echo '<p>FTP Connection Status: Not Connected</p>';
}
}
if ( $write_result ) {
$read_result = $wp_filesystem->get_contents( $test_file );
echo '<p>Read Test: ' . ( 'test' === $read_result ? 'Success' : 'Failed' ) . '</p>';
$delete_result = $wp_filesystem->delete( $test_file, false, 'f' );
echo '<p>Delete Test: ' . ( $delete_result ? 'Success' : 'Failed' ) . '</p>';
}
} catch ( Exception $e ) {
echo '<p>File Operation Error: ' . $e->getMessage() . '</p>';
echo '<p>Stack trace:</p>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}
if ( isset( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
echo '<h3>Filesystem Errors:</h3>';
echo '<pre>';
print_r( $wp_filesystem->errors->get_error_messages() );
echo '</pre>';
}
} else {
echo '<p>Filesystem object not properly initialized</p>';
}
echo '<h2>PHP Information</h2>';
echo '<p>PHP Version: ' . phpversion() . '</p>';
echo '<p>FTP Extension Loaded: ' . ( extension_loaded( 'ftp' ) ? 'Yes' : 'No' ) . '</p>';
echo '<h2>WordPress Information</h2>';
echo '<p>WordPress Version: ' . get_bloginfo( 'version' ) . '</p>';
echo '<p>ABSPATH: ' . ABSPATH . '</p>';
echo '<p>WP_CONTENT_DIR: ' . WP_CONTENT_DIR . '</p>';
}
debug_ftp_connection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment