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

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.

Related

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.

Seperate file name from path glob() [duplicate]

This question already has answers here:
How do I get a file name from a full path with PHP?
(14 answers)
Closed 6 years ago.
How do I get only the file name instead of the path + file name?
foreach(glob('./item/*.txt') as $filename){
echo $filename;
}
Use basename($filename) to get the filename of the file. Check the documentation here: http://php.net/manual/en/function.basename.php

How do I get the end of a file path in php? [duplicate]

This question already has answers here:
How to get the last dir from a path in a string
(11 answers)
Closed 6 years ago.
So I have a file path as a text variable in php:
"one/cool/file/path"
I need a php code to return "path"
By return I think you mean display...
Lets suppose you have the following path:
"one/cool/file/path"
Stored as a string in the variable:
"path"
To return it (or display it) you can just do:
echo path;
If you then go to your php in a web browser you should see your parh there!
You could achieve this using the following method:
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
Output: /www/htdocs/inc
http://php.net/manual/en/function.pathinfo.php#example-2657

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

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 .

How can I get the name of the current directory - not the whole path? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get the last dir from a path in a string
I'm using getcwd() to return something like this: home/abc123/public_html/blah/myDir
How can I modify this string to simply return myDir?
http://php.net/manual/en/function.basename.php
Either use basename:
print basename("home/abc123/public_html/blah/myDir");
//Output: myDir
Or strpos and substr:
$fullPath = "home/abc123/public_html/blah/myDir";
print substr($fullPath, strrpos($fullPath, "/") + 1);
//Output: myDir

Categories