Why is it whenever I use scandir() I receive periods at the beginning of the array?
Those are the current (.
) and parent (..
) directories. They are present in all directories, and are used to refer to the directory itself and its direct parent.
why there is '.' and '..' when we scandir function
They are directories. The . is the current directory and the .. is the parent directory
You can try cd into this directories like this:
cd .
What is wrong in this way of using PHP scandir() function?
.
is the current directory, ..
is the parent directory. You can filter those out like this:
$directories = scandir("./images");
$carpet_images = array_filter($directories, function($var) {
return !in_array($var, ['.', '..']);
});
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()
PHP Scandir returns extra periods
'.' and '..' are returned for current and parent directory. You can filter them:
while (false !== ($filename = readdir($dh))) {
if ($filename != '.' && $filename != '..')
$files[] = $filename;
}
Upload script add's dots in array by default
That's the parent directory
and the previous directory
You can see this behavior also, when you do a simple directory structure listing in cmd:
C:\data>dir
Volume in drive C has no label.
Volume Serial Number is CEAE-5F97
Directory of C:\data
12.01.2017 17:02 .
12.01.2017 17:02 ..
27.12.2016 11:10 db
08.12.2016 14:46 keys
01.12.2016 11:08 531 368 putty.exe
11.01.2017 13:00 www
1 File(s) 531 368 bytes
5 Dir(s) 133 482 266 624 bytes free
You can just unset those values, when using directory scans.
How to properly use scandir() in c?
The function scandir()
allocates the memory for you.
You do not need to allocate ANY memory. You DO need to free the memory returned to you by scandir()
.
Your code calls: *noOfFiles = scandir(path, &fileListTemp, NULL, alphasort);
On return, noOfFiles
will contain the number of directory entries in the path
directory, and fileListTemp
will point to an allocated array of pointers to allocated struct dirent blobs each of which has a d_name member which points to the null-terminated name of a file/directory.
If your directory contains the files "FirstFile.txt", "AnotherFile.txt", "ThirdFile.txt", for example, with your call, upon return from scandir()
, noOfFiles
will be set to 5 for the three files plus two more for the "." and ".." directory entries. THE ENTRIES WILL BE IN NO PARTICULAR ORDER IF YOU DO NOT PASS 'alphasort'. (Actually that's a little incorrect. They will be in the order of the directory filename entries which depends on the order in which the files were originally created.)
Because you passed 'alphasort' you should see the entries in the following order (I am explicitly showing the null-byte-string-terminator:
fileListTemp[0]->d_name == ".\0"
fileListTemp[1]->d_name == "..\0"
fileListTemp[2]->d_name == "AnotherFile.txt\0"
fileListTemp[3]->d_name == "FirstFile.txt\0"
fileListTemp[4]->d_name == "ThirdFile.txt\0"
So fileListTemp points to a block of allocated memory holding five struct dirent pointers. Each of the five struct dirent pointers points to a struct dirent block of allocated memory containing a null-terminated directory entry name in the d_name member. (Even this is a simplification, because the d_name entry is also a pointer, but it points to extra allocated space at the tail end of the allocated block, and the entry name is stored there.)
That is SIX blocks of allocated memory.
You can use this allocated memory until you are done with it, and then you call free() on EACH entry in the array followed by free() of the array itself.
You MUST free every entry as well as the array itself. They are all independently allocated blobs of memory.
When you are done with the list you should:
for (int i = 0; i < noOfFiles; i++)
{
free(fileListTemp[i];
}
free(fileListTemp);
remove dot and double dot from result php
foreach ($files as &$value) {
if ($value != '.' && $value != '..')
echo $myDirectory.'\\'.$value.'<br>';
}
Related Topics
Highlight the Word in the String, If It Contains the Keyword
Guzzle: Handle 400 Bad Request
Excel Date Conversion Using PHP Excel
Parsing JSON Array with PHP Foreach
PHP How to Import All Classes from Another Namespace
Laravel 5.4 Field Doesn't Have a Default Value
Redirecting to Authentication Dialog - "An Error Occurred. Please Try Again Later"
Tcpdf Utf-8 Symbols Not Showing Up
How to Pass Variables as Stdin into Command Line from PHP
Apache Mod Rewrite for Laravel
How to Insert an Array into a Single MySQL Prepared Statement W/ PHP and Pdo
Php: Get N-Th Item of an Associative Array
PHP Alternative to Session_Is_Registered