Read directory?

how to read all files in folder and save their name in databas …
i want that in a input field i give folder name that that read all files in that folder and return their complete name with extention and that i can save each name separate in database

Read this http://php.net/manual/en/function.opendir.php


<?php

$directory = '.';

foreach (new DirectoryIterator($directory) as $fileInfo) {

	// Skip if it is a . or ..
    if($fileInfo->isDot())
	{
		continue;
	}
	// Do what you want with the file names here.
    echo $fileInfo->getFilename();
	echo '<br />';
}

$dir = “directoryname”;
$open = opendir($dir);

while(false !== ($read = readdir($open))){
if($read != ‘.’ && $read != ‘…’){

echo $read."&lt;br /&gt;";}
}

what abt this one which is more better and faster

because i don’t have much idea about OOP

What you have is fine but its better to have error handling aswell to make sure you can debug problems

$dir = "directoryname";

if (is_dir($dir)){
    if ($open = opendir($dir)){
        while(false !== ($read = readdir($open))){
            if ($read != '.' && $read != '..'){
                echo $read."<br />";
            }
        }
    } else {
        echo "Unable to open the directory at \\"$dir\\"";
    }
} else {
    echo "The directory ( $dir ) is not a valid path!";
}

after writing this code


$dir = "A Flat";
$open = opendir($dir);

while(false !== ($read = readdir($open))){
    if($read != '.' && $read != '..'){
        
        
    echo $read."<br />";}
    }

out is this

01 - Meetha Sa Ishq.mp3
02 - Dil Kashi.mp3
03 - Chal Halke Halke.mp3
04 - Pyar Itna Na Kar.mp3
05 - Meetha Sa (unplugged).mp3
06 - Dil Kashi.mp3
07 - Meetha Sa (partymap mix).mp3

full file name
i will insert these data in table song in link column
but i need also the name of songs for inserting in name colum which i show on page to show people name of songs
because if the same name i put in name field so .mp3 will be with every name that will not look good
so how i add also separete name of these songs which will be in directory

$dir = "A Flat";
$open = opendir($dir);

while(false !== ($read = readdir($open))){
    if($read != '.' && $read != '..'){
        
    
    echo $read;
    
    }
    }

how to get filesize of every file in folder as m gettiing name of every song separetly ? any function for getting size ? filesize is not working on $read

Try this

if (is_dir($dir)){
    if ($open = opendir($dir)){
        while(false !== ($read = readdir($open))){
            if ($read != '.' && $read != '..'){
                echo $read." - File Size: ".filesize($dir."/".$read)." bytes<br />";
            }
        }
    } else {
        echo "Unable to open the directory at \\"$dir\\"";
    }
} else {
    echo "The directory ( $dir ) is not a valid path!";
}

thanks :slight_smile:

No problem