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.
Related
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];
This question already has answers here:
Recursive File Search (PHP)
(9 answers)
Closed 8 years ago.
I'm trying to create a function that will efficiently search the document root for a file and return its path. Currently I have the following thanks to the RecursiveDirectoryIterator and RegexIterator.
<?php
function find_file($filename) {
$pattern = preg_quote($filename);
$dir = new RecursiveDirectoryIterator($_SERVER['DOCUMENT_ROOT']);
$iterate = new RecursiveIteratorIterator($dir);
$reg_iterate = new RegexIterator($iterate, "/.*$pattern.*/", RegexIterator::GET_MATCH);
$ignore_pattern = '(\/?)(_*?)(old|archive|backup|cache|node_modules)';
foreach($reg_iterate as $matches){
foreach($matches as $file){
$file = rtrim($file, '.');
if(!preg_match("/$ignore_pattern/i", $file)){
return $file;
}
}
}
}
echo find_file('styles.css');
?>
I'm trying though to ignore certain directories from searching, for performance reasons. I'd rather not be scanning through directories that are known to be fruitless. Currently, I can only ignore certain patterns of paths after receiving the list of files from RecursiveIteratorIterator, so it's slow. Is there a smarter method you might recommend?
You could try glob: http://php.net/manual/en/function.glob.php. Easy to use and it's fast.
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 .
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);
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.