Here is my code to get a list of the file URLs inside a specific directory:
PHP
<?php
function getDirContents($directories, &$results = array()){
$files = array_diff(scandir($directories), array('..', '.'));;
foreach($files as $key => $value){
$path = $directories.DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
getDirContents($path, $results);
} else {
$directory_path = basename($_SERVER['REQUEST_URI']);
$results[] = 'https://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
}
}
return $results;
}
$directories = "directory_A";
echo json_encode(getDirContents($directories));
The result for directory_A:
https://example.com/directory_A/voice1.mp3
https://example.com/directory_A/Media/inside_media_1/voice2.mp3
https://example.com/directory_A/Media/inside_media_1/voice3.mp3
What if we want to get URLs from multiple directories?
I tried to loop through those directories using a for loop like this:
<?php
function getDirContents($directories, &$results = array()){
$length = count($directories);
for ($i = 0; $i < $length; $i++) {
$files = array_diff(scandir($directories[$i]), array('..', '.'));;
foreach($files as $key => $value){
$path = $directories[$i].DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
getDirContents($path, $results);
} else {
$directory_path = basename($_SERVER['REQUEST_URI']);
$results[] = 'https://' . $_SERVER['SERVER_NAME'] . str_replace($directory_path, "", $_SERVER['REQUEST_URI']) .$path;
}
}
}
return $results;
}
$directories = array("directory_A", "directory_B");
echo json_encode(getDirContents($directories));
But I only get the files inside those directories and code dosn't go to the folders inside each directory so I only get this for directory_A:
https://example.com/directory_A/voice1.mp3
What I'm missing and how to fix this?
You are passing an array and a string to the function. You have two options. You can always send in an array, or you can look for a string and convert it to an array.
class GoDir
{
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$directories = array("storage", "config");
echo json_encode($this->getDirContents($directories));
}
public function getDirContents($directories, &$results = array())
{
// See if we got a string and convert to array
if ( is_string($directories) ) {
$directories = [$directories];
}
$length = count($directories);
for ($i = 0; $i < $length; $i++) {
$files = array_diff(scandir($directories[$i]), array('..', '.'));;
foreach($files as $key => $value){
$path = $directories[$i].DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
$this->getDirContents($path, $results);
} else {
$directory_path = '/';
$results[] = $directory_path . $path;
}
}
}
return $results;
}
// Here is an alternative solution:
public function getDirContents($directories, &$results = array())
{
$length = count($directories);
for ($i = 0; $i < $length; $i++) {
$files = array_diff(scandir($directories[$i]), array('..', '.'));;
foreach($files as $key => $value){
$path = $directories[$i].DIRECTORY_SEPARATOR.$value;
if(is_dir($path)) {
// Turn the param into an array
$this->getDirContents([$path], $results);
} else {
$directory_path = '/';
$results[] = $directory_path . $path;
}
}
}
return $results;
}
}
Edit
There is nothing wrong with my code so feel free to use it as it is, if ever you need to search your file system from within PHP then echo the results.
I created a class to search through files using glob. It worked perfectly but now that I have migrated from Apache to Nginx, it always returns 0 results.
Here is my code:
public static function search($path, $find, $caseSensitive = false)
{
if ($path[strlen($path) - 1] !== '/')
$path .= '/';
$path = '../'.$path;
$pathLen = strlen($path);
$path .= '*';
if ($caseSensitive)
$files = self::globRecursive($path.$find);
else
{
$findLen = strlen($find);
for ($i = 0; $i < $findLen; $i++)
$find1 .= '['.strtolower($find[$i]).strtoupper($find[$i]).']';
$files = self::globRecursive($path.$find1);
}
$message = '';
$count = count($files);
if ($count === 0)
return '"'.$find.'" not found.';
foreach ($files as $file)
$message .= substr($file, $pathLen).'<br />';
return '"'.$find.'" found in '.$count.' files:<br />'.$message;
}
private static function globRecursive($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
$files = array_merge($files, self::globRecursive($dir.'/'.basename($pattern), $flags));
return $files;
}
The problem was due to changing my root directy. Simply prepending ../ to my search query fixed the problem.
I am trying to create an option of uploading an avatar to my site. What I am trying to achieve is this :
first avatar : 1.jpg
second avatar : 2.jpg
third avatar : 3.png
and so on..
How can I create an upload counter in php? My current code is this :
if(!empty($_FILES['cover']['tmp_name']))
{
$uploadfolder = "avatar/";
$file1 = rands().'.'.end(explode('.',$_FILES['cover']['name']));
$cover = $uploadfolder.$file1;
move_uploaded_file($_FILES['cover']['tmp_name'],$cover);
}
else
{
$cover = ''
}
The function rands() does not do anything, so please use it to demonstrate how I can achieve my goal.
If you store your users in database and there is integer user ID, you better use this user ID for file naming rather than separate incrementing counter.
Also you can look at existing files to find maximum existing number like this:
function getNextFileName ()
{
$a = 0;
$b = 2147483647;
while ($a < $b)
{
$c = floor (($a + $b) / 2);
if (file_exists ("$c.jpg")) $a = $c + 1;
else $b = $c;
}
return "$a.jpg";
}
function saveAvatar ($avatar)
{
for ($i = 0; $i < 16; $i++)
{
$name = getNextFileName ();
$fd = fopen ($name, 'x');
if ($fd !== FALSE)
{
fwrite ($fd, $avatar);
fclose ($fd);
return $name;
}
}
return FALSE;
}
for ($i = 0; $i < 20; $i++)
saveAvatar ("BlahBlahBlah$i");
It seem you have problem in genetaing random numbers you can try this:
$prefix = substr(str_shuffle("0123456789"), 0,3);
$file1 = $prefix.'.'.end(explode('.',$_FILES['cover']['name']));
the above $prefix will be like : any random 3 digits
Hope will help it!
/*
* currentDir - path - eg. c:/xampp/htdocs/movies/uploads (no end slash)
* $dir - points current working directory.
* $filename - name of the file.
*/
public static function getFileName($dir, $filename) {
$filePath = $dir . "/uploads/" . $filename;
$fileInfo = pathinfo($filePath);
$i = 1;
while(file_exists($filePath)) {
$filePath = $dir . "/uploads/" . $fileInfo['filename'] . "_" . $i . "." . $fileInfo['extension'];
$i++;
}
return $filePath;
}
move_uploaded_file($_FILES['cover']['tmp_name'],$filePath);
if same filename existing in your uploads folder. it will auto generate the
avatar_1.jpg,
avatar_2.jpg,
avatar_3.jpg, ans so on ..
If (and this is a big IF) your server supports file locking, you can reasonably ensure you have a unique incrementing ID with:
function get_avatar_id()
{
$lockfile = fopen("avatar_id_lock_file","a");
if(flock($lockfile, LOCK_EX)) // Get an exclusive lock to avoid race conditions
{
$avatar_id = intval(file_get_contents("avatar_id"); // Assumes you made it and put a number in it
$avatar_id++;
file_put_contents("avatar_id", $avatar_id);
flock($lockfile, LOCK_UN);
fclose($lockfile);
return $avatar_id;
}
else
{
//What do you want to do if you can't lock the file?
}
}
create an XML file and store the count in there
<? xml version="1.0" ?>
<MyRootNode>
<count>123</count>
</MyRootNode>
UPDATE
Update added after you added more code to your question.
Function Rands(FileExtension as string) as long
'1 open xml file
'2 read counter in
'3 increment counter
'4 save value to back xml file
'5 return incremented counter with the file extension passed attached on the end
'This is in case a BMP GIF or PNG has been and not JPG
' SAMPLE filename 123.GIF
End Function
I have this function that checks for a filename. If it exists, it increments it by one following this patter:
image.jpg
image1.jpg
image2.jpg
The problem comes on the 4th image, it comes back with 0.jpg.
Here is the relevant code:
...
$filetarget = $this->make_image_filename($directory, $new_filename, $extension);
if(!move_uploaded_file($file['tmp_name'], $filetarget)){
$error[$index] = 'copy';
}
...
private function make_image_filename($directory, $name = '', $extension){
if(empty($name)) $name = 'NULL';
$filetarget = $directory.$name.$extension;
if(file_exists($filetarget)){
$name = $this->increment_filename($name);
return $this->make_image_filename($directory, $name, $extension);
} else {
return $filetarget;
}
}
private function increment_filename($name){
$index = $this->get_filename_index($name);
if(is_numeric($index)){
$pos = strpos($name, $index);
$name = substr($name, 0, $pos);
}
if(is_null($index)){
$index = 0;
}
++$index;
return $name.$index;
}
private function get_filename_index($name){
// CHECK FOR INDEX
$i = 1;
$index = substr($name, -$i);
$last_chars = substr($name, -$i);
while(is_numeric($last_chars)){
++$i;
$last_chars = substr($name, -$i);
if(is_numeric($last_chars)){
$index = $last_chars;
}
}
if(is_numeric($index)){
return $index;
} else {
return NULL;
}
}
I am in the process now of isolating this code on my local server to run some tests. Can you see anything inherently flawed in this process?
Here is a function I use to do the same thing:
function make_unique($full_path) {
$file_name = basename($full_path);
$directory = dirname($full_path).DIRECTORY_SEPARATOR;
$i = 2;
while (file_exists($directory.$file_name)) {
$parts = explode('.', $file_name);
// Remove any numbers in brackets in the file name
$parts[0] = preg_replace('/\(([0-9]*)\)$/', '', $parts[0]);
$parts[0] .= '('.$i.')';
$new_file_name = implode('.', $parts);
if (!file_exists($new_file_name)) {
$file_name = $new_file_name;
}
$i++;
}
return $directory.$file_name;
}
(except it make file names like image(1).jpg image(2).jpg)
How about this:
function get_next_file_name($file) {
if (!preg_match('/^(\D+)(\d*)(\.\S+)$/', $file, $match)) {
throw new Exception('bad file name format');
}
return $match[1] . (empty($match[2]) ? 1 : ($match[2] + 1)) . $match[3];
}
echo get_next_file_name('image.jpg'), "\n"; // prints image1.jpg
echo get_next_file_name('image1.jpg'), "\n"; // prints image2.jpg
echo get_next_file_name('image999.jpg'), "\n"; // prints image1000.jpg
function foldersize($path) {
$total_size = 0;
$files = scandir($path);
foreach($files as $t) {
if (is_dir(rtrim($path, '/') . '/' . $t)) {
if ($t<>"." && $t<>"..") {
$size = foldersize(rtrim($path, '/') . '/' . $t);
$total_size += $size;
}
} else {
$size = filesize(rtrim($path, '/') . '/' . $t);
$total_size += $size;
}
}
return $total_size;
}
function format_size($size) {
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}
$SIZE_LIMIT = 5368709120; // 5 GB
$sql="select * from users order by id";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)) {
$disk_used = foldersize("C:/xampp/htdocs/freehosting/".$row['name']);
$disk_remaining = $SIZE_LIMIT - $disk_used;
print 'Name: ' . $row['name'] . '<br>';
print 'diskspace used: ' . format_size($disk_used) . '<br>';
print 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>';
}
php disk_total_space
Any idea why the processor usage shoot up too high or 100% till the script execution is finish ? Can anything be done to optimize it? or is there any other alternative way to check folder and folders inside it size?
function GetDirectorySize($path){
$bytestotal = 0;
$path = realpath($path);
if($path!==false && $path!='' && file_exists($path)){
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
$bytestotal += $object->getSize();
}
}
return $bytestotal;
}
The same idea as Janith Chinthana suggested.
With a few fixes:
Converts $path to realpath
Performs iteration only if path is valid and folder exists
Skips . and .. files
Optimized for performance
The following are other solutions offered elsewhere:
If on a Windows Host:
<?
$f = 'f:/www/docs';
$obj = new COM ( 'scripting.filesystemobject' );
if ( is_object ( $obj ) )
{
$ref = $obj->getfolder ( $f );
echo 'Directory: ' . $f . ' => Size: ' . $ref->size;
$obj = null;
}
else
{
echo 'can not create object';
}
?>
Else, if on a Linux Host:
<?
$f = './path/directory';
$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
$size = fgets ( $io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
echo 'Directory: ' . $f . ' => Size: ' . $size;
?>
directory size using php filesize and RecursiveIteratorIterator.
This works with any platform which is having php 5 or higher version.
/**
* Get the directory size
* #param string $directory
* #return integer
*/
function dirSize($directory) {
$size = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
$size+=$file->getSize();
}
return $size;
}
A pure php example.
<?php
$units = explode(' ', 'B KB MB GB TB PB');
$SIZE_LIMIT = 5368709120; // 5 GB
$disk_used = foldersize("/webData/users/vdbuilder#yahoo.com");
$disk_remaining = $SIZE_LIMIT - $disk_used;
echo("<html><body>");
echo('diskspace used: ' . format_size($disk_used) . '<br>');
echo( 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>');
echo("</body></html>");
function foldersize($path) {
$total_size = 0;
$files = scandir($path);
$cleanPath = rtrim($path, '/'). '/';
foreach($files as $t) {
if ($t<>"." && $t<>"..") {
$currentFile = $cleanPath . $t;
if (is_dir($currentFile)) {
$size = foldersize($currentFile);
$total_size += $size;
}
else {
$size = filesize($currentFile);
$total_size += $size;
}
}
}
return $total_size;
}
function format_size($size) {
global $units;
$mod = 1024;
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
$endIndex = strpos($size, ".")+3;
return substr( $size, 0, $endIndex).' '.$units[$i];
}
?>
function get_dir_size($directory){
$size = 0;
$files = glob($directory.'/*');
foreach($files as $path){
is_file($path) && $size += filesize($path);
is_dir($path) && $size += get_dir_size($path);
}
return $size;
}
Thanks to Jonathan Sampson, Adam Pierce and Janith Chinthana I did this one checking for most performant way to get the directory size. Should work on Windows and Linux Hosts.
static function getTotalSize($dir)
{
$dir = rtrim(str_replace('\\', '/', $dir), '/');
if (is_dir($dir) === true) {
$totalSize = 0;
$os = strtoupper(substr(PHP_OS, 0, 3));
// If on a Unix Host (Linux, Mac OS)
if ($os !== 'WIN') {
$io = popen('/usr/bin/du -sb ' . $dir, 'r');
if ($io !== false) {
$totalSize = intval(fgets($io, 80));
pclose($io);
return $totalSize;
}
}
// If on a Windows Host (WIN32, WINNT, Windows)
if ($os === 'WIN' && extension_loaded('com_dotnet')) {
$obj = new \COM('scripting.filesystemobject');
if (is_object($obj)) {
$ref = $obj->getfolder($dir);
$totalSize = $ref->size;
$obj = null;
return $totalSize;
}
}
// If System calls did't work, use slower PHP 5
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
foreach ($files as $file) {
$totalSize += $file->getSize();
}
return $totalSize;
} else if (is_file($dir) === true) {
return filesize($dir);
}
}
Even though there are already many many answers to this post, I feel I have to add another option for unix hosts that only returns the sum of all file sizes in the directory (recursively).
If you look at Jonathan's answer he uses the du command. This command will return the total directory size but the pure PHP solutions posted by others here will return the sum of all file sizes. Big difference!
What to look out for
When running du on a newly created directory, it may return 4K instead of 0. This may even get more confusing after having deleted files from the directory in question, having du reporting a total directory size that does not correspond to the sum of the sizes of the files within it. Why? The command du returns a report based on some file settings, as Hermann Ingjaldsson commented on this post.
The solution
To form a solution that behaves like some of the PHP-only scripts posted here, you can use ls command and pipe it to awk like this:
ls -ltrR /path/to/dir |awk '{print \$5}'|awk 'BEGIN{sum=0} {sum=sum+\$1} END {print sum}'
As a PHP function you could use something like this:
function getDirectorySize( $path )
{
if( !is_dir( $path ) ) {
return 0;
}
$path = strval( $path );
$io = popen( "ls -ltrR {$path} |awk '{print \$5}'|awk 'BEGIN{sum=0} {sum=sum+\$1} END {print sum}'", 'r' );
$size = intval( fgets( $io, 80 ) );
pclose( $io );
return $size;
}
I found this approach to be shorter and more compatible. The Mac OS X version of "du" doesn't support the -b (or --bytes) option for some reason, so this sticks to the more-compatible -k option.
$file_directory = './directory/path';
$output = exec('du -sk ' . $file_directory);
$filesize = trim(str_replace($file_directory, '', $output)) * 1024;
Returns the $filesize in bytes.
Johnathan Sampson's Linux example didn't work so good for me. Here's an improved version:
function getDirSize($path)
{
$io = popen('/usr/bin/du -sb '.$path, 'r');
$size = intval(fgets($io,80));
pclose($io);
return $size;
}
It works perfectly fine .
public static function folderSize($dir)
{
$size = 0;
foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
$func_name = __FUNCTION__;
$size += is_file($each) ? filesize($each) : static::$func_name($each);
}
return $size;
}
There are several things you could do to optimise the script - but maximum success would make it IO-bound rather than CPU-bound:
Calculate rtrim($path, '/') outside the loop.
make if ($t<>"." && $t<>"..") the outer test - it doesn't need to stat the path
Calculate rtrim($path, '/') . '/' . $t once per loop - inside 2) and taking 1) into account.
Calculate explode(' ','B KB MB GB TB PB'); once rather than each call?
PHP get directory size (with FTP access)
After hard work, this code works great!!!! and I want to share with the community (by MundialSYS)
function dirFTPSize($ftpStream, $dir) {
$size = 0;
$files = ftp_nlist($ftpStream, $dir);
foreach ($files as $remoteFile) {
if(preg_match('/.*\/\.\.$/', $remoteFile) || preg_match('/.*\/\.$/', $remoteFile)){
continue;
}
$sizeTemp = ftp_size($ftpStream, $remoteFile);
if ($sizeTemp > 0) {
$size += $sizeTemp;
}elseif($sizeTemp == -1){//directorio
$size += dirFTPSize($ftpStream, $remoteFile);
}
}
return $size;
}
$hostname = '127.0.0.1'; // or 'ftp.domain.com'
$username = 'username';
$password = 'password';
$startdir = '/public_html'; // absolute path
$files = array();
$ftpStream = ftp_connect($hostname);
$login = ftp_login($ftpStream, $username, $password);
if (!$ftpStream) {
echo 'Wrong server!';
exit;
} else if (!$login) {
echo 'Wrong username/password!';
exit;
} else {
$size = dirFTPSize($ftpStream, $startdir);
}
echo number_format(($size / 1024 / 1024), 2, '.', '') . ' MB';
ftp_close($ftpStream);
Good code!
Fernando
Object Oriented Approach :
/**
* Returns a directory size
*
* #param string $directory
*
* #return int $size directory size in bytes
*
*/
function dir_size($directory)
{
$size = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file)
{
$size += $file->getSize();
}
return $size;
}
Fast and Furious Approach :
function dir_size2($dir)
{
$line = exec('du -sh ' . $dir);
$line = trim(str_replace($dir, '', $line));
return $line;
}
Code adjusted to access main directory and all sub folders within it. This would return the full directory size.
function get_dir_size($directory){
$size = 0;
$files= glob($directory.'/*');
foreach($files as $path){
is_file($path) && $size += filesize($path);
if (is_dir($path))
{
$size += get_dir_size($path);
}
}
return $size;
}
if you are hosted on Linux:
passthru('du -h -s ' . $DIRECTORY_PATH)
It's better than foreach
Regarding Johnathan Sampson's Linux example, watch out when you are doing an intval on the outcome of the "du" function, if the size is >2GB, it will keep showing 2GB.
Replace:
$totalSize = intval(fgets($io, 80));
by:
strtok(fgets($io, 80), " ");
supposed your "du" function returns the size separated with space followed by the directory/file name.
Just another function using native php functions.
function dirSize($dir)
{
$dirSize = 0;
if(!is_dir($dir)){return false;};
$files = scandir($dir);if(!$files){return false;}
$files = array_diff($files, array('.','..'));
foreach ($files as $file) {
if(is_dir("$dir/$file")){
$dirSize += dirSize("$dir/$file");
}else{
$dirSize += filesize("$dir/$file");
}
}
return $dirSize;
}
NOTE: this function returns the files sizes, NOT the size on disk
Evolved from Nate Haugs answer I created a short function for my project:
function uf_getDirSize($dir, $unit = 'm')
{
$dir = trim($dir, '/');
if (!is_dir($dir)) {
trigger_error("{$dir} not a folder/dir/path.", E_USER_WARNING);
return false;
}
if (!function_exists('exec')) {
trigger_error('The function exec() is not available.', E_USER_WARNING);
return false;
}
$output = exec('du -sb ' . $dir);
$filesize = (int) trim(str_replace($dir, '', $output));
switch ($unit) {
case 'g': $filesize = number_format($filesize / 1073741824, 3); break; // giga
case 'm': $filesize = number_format($filesize / 1048576, 1); break; // mega
case 'k': $filesize = number_format($filesize / 1024, 0); break; // kilo
case 'b': $filesize = number_format($filesize, 0); break; // byte
}
return ($filesize + 0);
}
A one-liner solution. Result in bytes.
$size=array_sum(array_map('filesize', glob("{$dir}/*.*")));
Added bonus: you can simply change the file mask to whatever you like, and count only certain files (eg by extension).
This is the simplest possible algorithm to find out directory size irrespective of the programming language you are using.
For PHP specific implementation. go to: Calculate Directory Size in PHP | Explained with Algorithm | Working Code