PHP file, directory, soft link and hard link information

What PHP functions file_exists, is_file, is_dir and is_link return for file, directory, hard link and soft link?

<?php

/*
touch file
mkdir dir
ln file file_hard_link
ln -s file file_soft_link
ln -s dir dir_soft_link
touch dir/file_in_dir
echo 1 > file
cat file file_hard_link file_soft_link
*/

$tests = [
    'file',
    'dir',
    'file_hard_link',
    'file_soft_link',
    'dir_soft_link',
    'dir/file_in_dir',
    'dir_soft_link/file_in_dir'
];

foreach ($tests as $test) {
    echo 'name: ', str_pad($test, 25) . ' ';
    echo 'file_exists: ', (int)file_exists($test) . ' ';
    echo 'is_file: ', (int)is_file($test)  . ' ';
    echo 'is_dir: ', (int)is_dir($test) . ' ';
    echo 'is_link: ', (int)is_link($test) . PHP_EOL;
}

Result:

name: file                      file_exists: 1 is_file: 1 is_dir: 0 is_link: 0
name: dir                       file_exists: 1 is_file: 0 is_dir: 1 is_link: 0
name: file_hard_link            file_exists: 1 is_file: 1 is_dir: 0 is_link: 0
name: file_soft_link            file_exists: 1 is_file: 1 is_dir: 0 is_link: 1
name: dir_soft_link             file_exists: 1 is_file: 0 is_dir: 1 is_link: 1
name: dir/file_in_dir           file_exists: 1 is_file: 1 is_dir: 0 is_link: 0
name: dir_soft_link/file_in_dir file_exists: 1 is_file: 1 is_dir: 0 is_link: 0

Leave a Reply

Your email address will not be published. Required fields are marked *