Include Just Files in Scandir Array

Include JUST files in scandir array?

You can use array_filter.

$indir = array_filter(scandir('../pages'), function($item) {
return !is_dir('../pages/' . $item);
});

Note this filters out all directories and leaves only files and symlinks. If you really want to only exclude only files (and directories) starting with ., then you could do something like:

$indir = array_filter(scandir('../pages'), function($item) {
return $item[0] !== '.';
});

scandir() returning only Array

Just do in this way..

<?php
$dir = "users/";
$fl = scandir($dir);
foreach (scandir($dir) as $fl)
echo $fl."<br>";
?>

Output like:

.
..
abc.txt

PHP: Using scandir(), folders are treated as files

You need to change directory or append it to your test. is_dir returns false when the file doesn't exist.

$scan = scandir('myFolder');

foreach($scan as $file)
{
if (!is_dir("myFolder/$file"))
{
echo $file.'\n';
}
}

That should do the right thing

How to get only images using scandir in PHP?

You can use glob

$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);

If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos, fnmatch or pathinfo to get the extension is up to you.

PHP scandir to find video files and not include subdirectories

You can try this way using glob() to find all the files in the directory /path/to/your/directory with a .mp4 file extension:

foreach (glob("/path/to/your/directory/*.mp4") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}

See Example :http://php.net/manual/en/function.glob.php

Scan dir and make array PHP

You can get your desired array with a simple foreach() like the following-

$dir = "/path/to/directory";
$scans = scandir($dir);
$episodes = array();

foreach($scans as $k=>$v){
if (preg_match('/.mp4$/', $v))
array_push($episodes,['id'=>$k,'filename'=>$v]);
}

at the end $episodes will contain the array. $dir need to be absolute or relative path to your directory.

Then loop through the episodes array to get individual episode.

<?php foreach ($episodes as $episode){?>
<?php echo $episode['filename']; ?>
<?php } ?>

php: scan a directory just for files (filename+extension)

You can use scandir too, of course. I'd suggest using RecursiveIteratorIterator. with RecursiveDirectoryIterator

Example code:

function recursiveDirectoryIterator($path) {
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file) {
if (!$file->isDir()) {
yield $file->getFilename(); // You can add $file->getFileExtension() too
}
}
}

Normally, I'd just leave this as a comment, but I feel most people don't use RecursiveIterator and DirecotryIterator very often, so an example was in order.

php scandir() not showing files - only showing directories

Scandir does not work recursively. It only scans the path input into it.

Scandir Recurrsive function to scan deeper into the file system

php scandir produces extra elements (2 dots)

. - is a special directory referencing the current directory.

.. - is also a special directory and its referencing the parent directory.

To remove the special directories I can think of some options:

1.

foreach(glob("*.php") as $filename) {
echo "<div>$filename</div>";
}

2.

$files = array_diff(scandir("content"), array('..', '.'));
foreach($files as $file) { ... }

3.

foreach ($files as $file) {    
if($file != '.' and $file != '..') { ... }
}

All of the above are alternatives. You don't need to use scandir() if you use glob() and vice versa. glob() - expects a pattern. It is possible to also provide it with the path like this:

glob("[path]/*.php") - this will list any php file located in path. glob() documentation can be found here PHP - glob()



Related Topics



Leave a reply



Submit