PHP files list in directory

If you wan to show all files and folders in a directory, use scandir(02-04-2013irectory) function.
Example:

<html>
<head>
<title>files list in directory</title>

<?php

$cdir=getcwd(); //get current directory
$con=scandir($cdir); //retrieve contents of directory
//print the contents
foreach($con as $v)
echo $v."<br>";

?>

</head>

<body>
</body>
</html>

To show only the files, you can is_file($file) function to check whether the $file argument is a file.
<html>
<head>
<title>files list</title>

<?php

$cdir=getcwd(); //get current directory
$con=scandir($cdir); //retrieve contents of directory
//print the contents
foreach($con as $v){
if(is_file($v))
echo $v."<br>";
}

?>

</head>

<body>
</body>
</html>

To show only the directories in the directory, you can is_dir($file) function to check whether the $file argument is a directory.

<html>
<head>
<title>directories list</title>

<?php

$cdir=getcwd(); //get current directory
$con=scandir($cdir); //retrieve contents of directory
//print the contents
foreach($con as $v){
if(is_dir($v))
echo $v."<br>";
}

?>

</head>

<body>
</body>
</html>

Posted by: Dara | post date: 02-04-2013 | Subject: PHP




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.