php


php count files from multiple folders and echo total


I have a php code that will display the amount of files that i have in a folder.
Code: This will echo this on my page, "There are a total of 119 Articles"
$directory = "../health/";
if (glob($directory . "*.php") != false) /* change php to the file you require either html php jpg png. */ {
$filecount = count(glob($directory . "*.php")); /* change php to the file you require either html php jpg png. */
echo "<p>There are a total of";
echo " $filecount ";
echo "Articles</p>";
} else {
echo 0;
}
Question:
I am wanting to count the files from 27 or more folders and echo the total amount of files.
Is there away i can add a list of folders to open such as:
$directory = "../health/","../food/","../sport/";
then it will count all the files and display the total "There are a total of 394 Articles"
Thanks
Yes you can:
glob('../{health,food,sport}/*.php', GLOB_BRACE);
Undoubtedly, this is less efficient than clover's answer:
$count = 0;
$dirs = array("../health/","../food/","../sport/");
foreach($dirs as $dir){
if($files = glob($dir."*.php")){
$count += count($files);
}
}
echo "There are a total of $count Articles";
A simple answer is to just use an array and a loop. It is something you could have figured out yourself.
$directories = array('../health/', '../food/', '../sport/');
$count = 0;
foreach ($directories as $dir) {
$files = glob("{$dir}*.php") ?: array();
$count += count($files);
}
echo "<p>There are a total of {$count} articles</p>";
But #clover's answer is better.
As usual, it's often much better to divide your problem. E.g.:
Obtain the files (See glob).
Count the files of a glob result (Write a function that takes care of two the FALSE and Array cases.).
Do the output (don't do the output inside the other code, do it at the end, use variables (as you already do, just separate the output)).
Some Example Code:
/**
* #param array|FALSE $mixed
* #return int
* #throws InvalidArgumentException
*/
function array_count($mixed) {
if (false === $mixed) {
return 0;
}
if (!is_array($mixed)) {
throw new InvalidArgumentException('Parameter must be FALSE or an array.');
}
return count($mixed);
}
$directories = array("health", "food", "string");
$pattern = sprintf('../{%s}/*.php', implode(',', $directories));
$files = glob($pattern, GLOB_BRACE);
$filecount = array_count($files);
echo "<p>There are a total of ", $filecount, " Article(s)</p>";
You could use the opendir command explained here:
http://www.php.net/manual/en/function.opendir.php
combined with the example shown on previous link:
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
Basically opening the folder you first go through and in a loop count every singel item that is not a folder.
Edit:
Seems like someone has given a simpler solution than this.

Related Links

PHP -> simple XML -> xPath not working
PHP Date Time Conversion French
How to check if condition while inserting the database
php curl 417 - Expectation Failed Post Request
Directory not created in the linux server by php code
How to sum same key values and combine other values in multidimentional array in PHP?
How to output the value of a selected array key [duplicate]
Lottery on wordpress using php and json
Laravel 5.2 Socialite Facebook API login redirect error
How exactly do a good use and implementation of PDO-SQL Server?
Passing data to SOAP Function with PHP
Curl Response handling with Guzzle 6.2 with Laravel 5.3
how php form id works, is my mapping is right?
Slim Image Cropper With Laravel 5.3
Mysql Add NULL Value in Field: [duplicate]
Which technologies would be the best choice for an online real time chat application? [closed]

Categories

HOME
office365
webview
tomcat
vagrant
yahoo-weather-api
softlayer
systemd
risk-management
whmcs
websocket
flurry
firebase-dynamic-links
cisco
port
graphdb
clish
xbox360
corona
heap-dump
fosuserbundle
tfsbuild
cpu-usage
pe
weex
candlestick-chart
firemonkey-style
function-points
dragula
morris.js
envoy
matlab-app-designer
instructions
rust-cargo
series
riotjs
ms-media-foundation
rails-postgresql
mv
microkernel
git-tfs
nomad
absolute
realstudio
rightnow-crm
dredd
.net-micro-framework
return-type
supertest
beeline
password-hash
openstack-glance
android-sharing
google-news
http-basic-authentication
stateless-session-bean
mpmovieplayercontroller
openquery
piping
perforce-integrate
noclassdeffounderror
distributed-cache
polymerfire
mnesia
ioctl
pack
cortana
grinder
decidable
largenumber
ora-04091
android-studio-import
360-degrees
kefir.js
console.log
whitespace-language
camus
debug-symbols
lifetime
rebar
squeezebox
git-reset
roxy-fileman
django-sites
principalcontext
iirf
virtual-channel
void-pointers
android-authenticator
sigar
mvcrecaptcha
boost-foreach
representation
office-automation
grails-validation
formal-semantics
xap
html-generation

Resources

Encrypt Message