简体   繁体   中英

PHP glob() to count all image-files with $variable directory doesn't work

with PHP, I need to count all images in a certain directory, given by a variable $filedir, and store this count in a variable $imagecount.

Therefor, I'm using the following:

$fileurl = $file->url();

This variable gives the path to the file's directory like: /mnt/web5/d1/87/52146187/htdocs/bahrain/content/1-pavilion/3-phoenix-dactylifera

$imagecount = count(glob($filedir . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE));

This should count all files of the type jpg, jpeg, png, gif

As a result, I'm always receiving "0" for $imagecount. Does anyone know, what the problem might be? (There are some given image-files in this directory)

Thanks!

Use chdir() to set your desired directory.

<?php
echo getcwd(),"<br>";  // where you start from
chdir($filedir);       // where you want to look
echo getcwd(),"<br>";  // see if you actually got there
$image_count=count(glob("*.{jpg,jpeg,png,gif}",GLOB_BRACE);
echo "$image_count";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM