0

I recently migrated a WordPress site over from PHP5 to PHP7. One of our custom file listing functions (outputs a listing of files from a folder on a server directory) is failing on the new server on PHP7. I know there is an argument conflict but I cannot figure out how to rectify it.

The error:

5 PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function printDirectory(), 3 passed in /nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc.php on line 1026 and exactly 4 expected in /nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc.php:1014\nStack trace:\n#0 /nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc.php(1026): printDirectory('https://lfccedu...', '/nas/content/li...', 'files/documents...')\n#1 /nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc.php(973): printDirectory('https://lfccedu...', '/nas/content/li...', 'files/documents...', 'ASC')\n#2 [internal function]: DirectoryList(Array, '', 'lfcc_directory_...')\n#3 /nas/content/live/lfccedu/wp-includes/shortcodes.php(325): call_user_func('DirectoryList', Array, '', 'lfcc_directory_...')\n#4 [internal function]: do_shortcode_tag(Array)\n#5 /nas/content/live/lfccedu/wp-includes/shortcodes.php(199): preg_replace_callback('/\\[(\\[?)(captio...', 'do_shortcode_ta...', '[caption id="at...')\n#6 [internal function]: do_shortcode('[caption id="at... in /nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc.php on line 1014

Here's the code:

function DirectoryList($atts)
{
    extract(shortcode_atts( array(
        'file_root' => '',
        'folder' => '',
        'sort' => 'ASC'
    ), $atts ));

    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domain = $_SERVER['HTTP_HOST'];
    return printDirectory($protocol . $domain, $file_root, $folder, $sort);
}

add_shortcode('lfcc_directory_list', 'DirectoryList');

function ScheduleUpdateTime()
{
    $aSettings = get_option('lfcc_external_db_settings');
    $sDateTime = (time() < strtotime(date("Y-m-d ".$aSettings['cache_time'],time()))) ? date("Y-m-d",(time()-(60*60*24))) : date("Y-m-d",time());
    $sDateTime .= ' ' . $aSettings['cache_time'];
    return date('M j, Y, \a\t g:i a', strtotime($sDateTime));
}

add_shortcode('lfcc_last_schedule_update', 'ScheduleUpdateTime');

function getFileList($root, $sort)
{
    $files = array();
    if (is_dir($root))
    {
        if ($handle = opendir($root)) 
        {
            while (false !== ($entry = readdir($handle))) 
            {
                if ($entry != "." && $entry != "..") {
                    $files[] = $entry;
                }
            }
        }
        natsort($files);
        if ($sort == 'DESC')
        {
            $files = array_reverse($files);
        }
    }
    return $files;
}

/**
 * Outputs a list of directories and files recursively.
 */
function printDirectory($url, $root, $path, $sort) 
{
    $sRtn = '';
    $current_dir = $root . '/' . $path;
    $files = getFileList($current_dir, $sort);
    foreach ($files as $f) 
    {
        $file_path = $current_dir . '/' . $f;
        if (is_dir($file_path)) 
        {
            // If it is a directory, output the directory name and look inside it for more
            $sRtn .= "<strong>" . $f . "</strong><ul>";
            $sRtn .= printDirectory($url, $root, $path . '/' . $f);
            $sRtn .= "</ul></li>";
        } 
        else 
        {
            // If it is a file, output a link to the file.
            $info = pathinfo($f);
            $sRtn .= '<li><a class="icon ' . $info['extension'] . '" href="' . $url . '/' . $path . '/' . $f . '" target="_blank">' . $info['filename'] . '</a></li>';
        }
    }
    return $sRtn;
} 


1

From the error message, it appears that your prior iteration made use of the looser nature of PHP5:

Too few arguments to function printDirectory(), 3 passed ... and exactly 4 expected in ...

I see that you define printDirectory() with four parameters:

  • $url
  • $root
  • $path
  • $sort

The first time you call this function, you pass all four parameters. Unfortunately, when you call it recursively (within the function itself) you only pass three of the four.

$sRtn .= printDirectory($url, $root, $path . '/' . $f);

Try passing the $sort variable in that recursive call.

  • Oh man can't believe I missed that. Thank you!!!!!! – Aaron Riddle Sep 14 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.