why there is '.' and '..' when we scandir function [duplicate] - php

This question already has answers here:
Why is it whenever I use scandir() I receive periods at the beginning of the array?
(6 answers)
Closed 9 years ago.
When we use scandir function.it will display the all the files and directories inside the directory,which we pass in the scandir function as argument.
But it also add two more element . and ..
I want to know why these two element is showing?

'.' is current directory
'..' is parent directory

They are directories. The . is the current directory and the .. is the parent directory
You can try cd into this directories like this:
cd .

Related

PHP Windows - Return drive letter from path [duplicate]

This question already has answers here:
PHP detect (or remove) current drive letter?
(2 answers)
Closed 5 years ago.
I'm trying to get the drive letter from a path in PHP windows.
I'm in this directory:
c:\Program Files\Adobe\
and I want to return:
c
What's the best way to do this? Is there an alternative to parse_url(), but for local paths (and usable under Windows?).
Use substr function from php.
$dir = 'c:/...';
$letter = substr($dir,0,1);
You could also use explode in php, which actually works similar to split.
$drive = explode(":",$path)[0];

PHP Count how many files in directory [duplicate]

This question already has answers here:
Count how many files in directory PHP
(15 answers)
Return total number of files within a folder using PHP
(10 answers)
Count number of files in folder in php
(6 answers)
How to count number of files in folder in php?
(6 answers)
Closed 5 years ago.
I was looking for some answers here but non of them worked for me what I have right now is
$directory = 'api\config\\';
$filecount = 0;
$files = glob($directory . '*.ini');
if ( $files !== false )
{
$filecount = count( $files );
echo $filecount;
}
The problem is $filecount is always 0
I'm using php 5.6 (required by the facility I'm working with).
the directory I want to use (..\api\config).
any ideas why is this not working with me?
You have forgotten to escape your first backslash. Use this:
$directory = 'api\\config\\';
One thing that will help for debugging in the future, is to try and output (echo) your $directory, to make sure the directory value is being passed to your functions correctly.

Best Way to Retrieve File Names in a Directory with a Specified Extension in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to get files from a dir filtered by certain extension in php
What would be the best way to retrieve file names with a specified extension?
I came up with this solution and it works fine but am wondering if there is a better way.
print_r(array_filter(scandir(dirname(__FILE__)), create_function('$a', 'return (substr($a, -4) == ".php") ? true : false ;')));
I would use glob. Maybe the most simplistic approach to this:
$files = glob( dirname(__FILE__) . '/*.php');
$files = array_map('basename', $files);
print_r($files);

PHP most efficient way to list the files in a very large directory [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Get the Files inside a directory
PHP: scandir() is too slow
I have a directory with tens of thousands of files in it and I want to display a list of these files on a page. I tried doing it with scandir and it takes forever. What would be an efficient method of achieving this?
I recommend using DirectoryIterator or RecursiveDirectoryIterator.
I haven't benchmarked them, but your other options are
glob() - http://php.net/manual/en/function.glob.php
opendir() - http://www.php.net/manual/en/function.opendir.php
$directory=opendir($_SERVER['DOCUMENT_ROOT'].'/directory/');
while ($file = readdir($directory)) {
if($file!="." && $file!=".."){
echo $file."<br>";
}
}
closedir($directory);

What is the "." parameter in php? [duplicate]

This question already has answers here:
What does a dot mean in a URL path?
(3 answers)
Closed 7 years ago.
<?php
$dit = new DirectoryIterator('.');
while ($dit->valid()) {
if (!$dit->isDot()) {
echo $dit->getFilename() . "\n";
}
$dit->next();
}
unset($dit);
?>
what does '.' parameter mean in DirectoryIterator method?
The argument tells it which directory to iterate over.
. is a relative path to "the current directory"
On every operating system . means the current directory, .. means the parent directory.

Categories