php to c++: working with a filesystem

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

This section covers the C++ equivalest of more or less the php function in the File System Related Extensions / Filesystem:

What is the C++ equivalent of the following PHP functions?


#include <boost/filesystem/operations.hpp>
#include "boost/filesystem/path.hpp"
#include <iostream>

namespace fs = boost::filesystem;

using std::cout;

int main( int argc, char *argv[]) {

        if (argc < 2) {
                cout << "\nUsage: run.is_dir [path] \n\n";
                return 1;

        if (fs::is_directory(argv[1]))
        //if (fs::is_directory("/usr"))
                cout << "OK\n";
                cout << "Not ok!\n";
        return 0;


1) POSIX (Linux) solution: use the function access().
See man 2 access (have the manpages-dev package installed).

2) Cross-platform solution: try to open the file for reading (with std::ifstream) and see if you get an error message. Or for directories, use boost::filesystem::directory_iterator.