Reputation: 1112
I'm writing a class for file hashing but I got stuck at the beginning.
class fileHashing
{
private $start_path = '../../../test';
private $log_path = '../../../test/log';
private $log_file = 'fileHash.xyz';
function __construct()
{
$this->open();
}
private function open() {
$files_list = scandir($this->start_path);
print_r($files_list);
date_default_timezone_set('Europe/Rome');
foreach ($files_list as $key => $element) {
if (is_file($element)) {
echo "FILE: " . $element . PHP_EOL;
} elseif (is_dir($element)) {
echo "DIR: " . $element . PHP_EOL;
} else {
echo "NONE: " . $element . PHP_EOL;
}
}
}
}
$a = new fileHashing();
Returns:
Array
(
[0] => .
[1] => ..
[2] => fileupload.php
[3] => log
[4] => test1.php
[5] => tsconfig.json
)
DIR: .
DIR: ..
FILE: fileupload.php
NONE: log
NONE: test1.php
NONE: tsconfig.json
log
is a folder and it's not recognized as it and both test1.php
and tsconfig.json
are not recognized as files.
Am I missing something about scandir()
/ is_file()
/ is_dir()
?
Upvotes: 1
Views: 966
Reputation: 15629
You should also append the start_path
to your filename inside is_file
and is_dir
. Could be done like this
private function open() {
$files_list = scandir($this->start_path);
print_r($files_list);
date_default_timezone_set('Europe/Rome');
foreach ($files_list as $key => $element) {
$file = $this->start_path . '/' . $element;
if (is_file($file)) {
echo "FILE: " . $element . PHP_EOL;
} elseif (is_dir($file)) {
echo "DIR: " . $element . PHP_EOL;
} else {
echo "NONE: " . $element . PHP_EOL;
}
}
}
Upvotes: 1
Reputation: 924
Use this way:-
if (filetype($this->start_path . '/' . $element) == "dir") {
// dir
}elseif(filetype($this->start_path . '/' . $element) == "file") {
// file
}
// or this way
if (is_dir($this->start_path . '/' . $element)) {
// dir
}elseif(is_file($this->start_path . '/' . $element)) {
// file
}
Upvotes: 1