(new Soapbox())->shout(array_map('strtoupper', $opinions)); //Shaun's blog


Me, elsewhere

GitHub
parseword
Miscellaneous public code

Twitter
@parseword
I don't tweet much

XMPP chat
xmpp@shaunc.com
(Pidgin, Miranda, Swift, etc.)


Perfect is the enemy of good enough.

Resolving PHP error "Warning: filetype(): Lstat failed for..."

Posted July 10, 2014 by shaun

When working with PHP's filetype() function, you may encounter the following error:

Warning: filetype(): Lstat failed for [filename]

This warning means that the argument to filetype() doesn't exist. Consider the following code, which is intended to get the contents of the system's /tmp directory while ignoring any subdirectories:

<?php
function get_file_list($directory) {
    $files = [];
    if (is_dir($directory) && $handle = opendir($directory)) {
        while (($file = readdir($handle)) !== false) {
            if (filetype($directory . $file) === 'file') {
                $files[] = $directory . $file;
            }
        }
        closedir($handle);
    }
    return $files;
}

var_dump(get_file_list('/tmp'));

This code will generate one Warning: filetype(): Lstat failed... for every file in the /tmp directory, and the returned $files array will be empty. Can you spot the error? The value of the $directory variable doesn't end with a slash, so the filetype() function is being called with arguments like /tmpmylog.txt instead of /tmp/mylog.txt. Each time filetype() encounters a nonexistent path, it outputs a warning message.

To fix the problem, the code should guarantee that a valid directory separator exists between the directory name and the filename. Here, strpos() is used to detect when a missing slash must be added:

function get_file_list($directory) {
    $files = [];
    if (strpos($directory, '/', -1) === false) {
        $directory = $directory . '/';
    }
    if (is_dir($directory) && $handle = opendir($directory)) {
        while (($file = readdir($handle)) !== false) {
            if (filetype($directory . $file) === 'file') {
                $files[] = $directory . $file;
            }
        }
        closedir($handle);
    }
    return $files;
}

This code will work as intended, without Warning: filetype(): Lstat failed... errors.



Recent articles

📰 Using WITH_META_MODE and ccache for FreeBSD build boosts

📰 Resolving subversion error E000013: Unable to create pristine install stream

📰 Enhancements to SmokePing's AnotherDNS probe

📰 Generating vanity DNSSEC key tags

📰 DDoS involving forged packets from 23.225.141.70

📰 Website integrity monitoring through version control

📰 SpamAssassin 3.4.2 fixes security problems, adds HashBL and phishing plugins

📰 Bug or turf war? ICQ via Pidgin now fails with "startOSCARSession: Request Timeout"

📰 🎂

📰 SFSQuery, a PHP class to query the StopForumSpam API and DNSBL

📰 Resolving portmaster error "pkg-static: automake-1.16.1 conflicts with automake-wrapper-20131203"

📰 Resolving LibreNMS error "RuntimeException: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths"

📰 1.1.1.1: Fast, but not so accurate (yet)

📰 autodiscover.xml as an Indicator of Attack

📰 Blocking Facebook's Tracking and Surveillance: A Comprehensive Approach

▲ Back to top | Permalink to this page