Listing all files and directories using PHP

Spread the love

Have you ever wondered how without an index file all files and directories are appearing in your web root? Or in any directory. Well it is just a CGI script doing that on the server most of the cases. With PHP, you can simply do that. There are lots of way of doing so but today I will be showing the most simple way using a built-in function named glob().

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells. Manual to read: http://php.net/manual/en/function.glob.php

So here is the code that will show you how to do it:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>TechnoCrews Kitchen</title>
</head>
<body>
	<?php echo "It Works!"; ?>

	<h1>Here are some cool projects by us</h1>
	<?php 
			//path to the directory to search/scan
			$directory = "";
			 //echo "$directory"
			//get all files in a directory. If any specific extension needed just have to put the .extension
			$local = glob($directory . "*"); 
			//print each file name
			echo "<ul>";

			foreach($local as $item)
			{
			echo '<li><a href="'.$item.'">'.$item.'</a></li>';
			}

			echo "</ul>";
	 ?>
</body>
</html>

Okay, breaking down the code into pieces for you.

$directory variable is to find out the files and directory on that directory. You can give any server based path here. I put it empty to show all files and folder of current directory of the PHP  script that is running this piece of code.

$local variable is getting the output of the glob function. In the glob function you will see I have appended “*” which means show all files and folders. If you want to find any specific file with certain extension you have to put the code just as below.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>TechnoCrews Kitchen</title>
</head>
<body>
	<?php echo "It Works!"; ?>

	<h1>Here are some cool projects by us</h1>
	<?php 
			//path to the directory to search/scan
			$directory = "";
			 //echo "$directory"
			//get all files in a directory. If any specific extension needed just have to put the .extension
			$local = glob($directory . "*.php"); 
			//print each file name
			echo "<ul>";

			foreach($local as $item)
			{
			echo '<li><a href="'.$item.'">'.$item.'</a></li>';
			}

			echo "</ul>";
	 ?>
</body>
</html>

This particular code will only show files with a .php extension. Execute at your own risk buddies 🙂

Now if you want more than one extension then you can use the second parameter. Let me show you:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>TechnoCrews Kitchen</title>
</head>
<body>
	<?php echo "It Works!"; ?>

	<h1>Here are some cool projects by us</h1>
	<?php 
			//path to the directory to search/scan
			$directory = "";
			 //echo "$directory"
			//get all files in a directory. If any specific extension needed just have to put the .extension
			//$local = glob($directory . "*"); 
			$local = glob("" . $directory . "{*.jpg,*.gif,*.png}", GLOB_BRACE);
			//print each file name
			echo "<ul>";

			foreach($local as $item)
			{
			echo '<li><a href="'.$item.'">'.$item.'</a></li>';
			}

			echo "</ul>";
	 ?>
</body>
</html>

Here we have put the extension in curly braces and used a FLAG named Glob Brace

GLOB_BRACE

It actually shows the file extension from the braces. The final foreach() in all example code above is actually printing the file and directory name one by one. I have just made a link to them with an <a> tag!

So, that’s it. So simple isn’t it? You may have lots of other method which will tear your head’s neuron apart. But this simple tricks can help you a lot to show files and folder in a directory. Hope you have enjoyed. Comment below if you had any problem or you have something new to share about. I would love to hear from you !

 


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Articles

বাংলাদেশ রেলওয়ের অনলাইন টিকেট বুকিং এর বিভিন্ন টিকেট ক্লাস

Spread the love

Spread the loveআজ বাংলাদেশ রেলওয়ের টিকেট কিনলাম অনলাইনে। একটা প্রোগ্রাম এটেন্ড করতে যাব প্লাস কিছু গেস্ট আসবে এই কারনে। তো অনলাইনে টিকেট কিনতে যাবার পর ঘটলো বিপত্তি। কারন আমি নরমালি ৩ টা সিট নিয়ে ধারনা রাখি। শোভন চেয়ার সুলভ আর স্নিগ্ধা তো যখন টিকেট কিনতে


Spread the love

Yoda Style Conditionals ( Yoda Condition) Explained (Bengali/বাংলায়)

Spread the love

Spread the loveআমরা যারা প্রোগ্রামিং কিংবা ডেভেলপমেন্ট করি তাদের অনেকসময় সবকিছু ঠিকঠাকমত কন্ডিশন লেখার পরও দেখা যায় প্রোগ্রাম ভুল বিহেভ করে। এর পিছনে কিন্তু বিশাল একটা কিছুর হাত আছে। আমরা যখন কোন অপারেশন চালাই সেটা যদি ঠিকমত ঘটে তাহলে তাকে প্রোগ্রামিং এর ভাষায় ট্রু কেইস


Spread the love

দুই তিন মাসেই হয়ে যান লাখপতি, অনলাইন ক্যারিয়ারের মাধ্যমে! কতখানি সম্ভব?

Spread the love

Spread the loveএকটা কমন ট্রেন্ড লক্ষ্য করছি যে, সবাই শেষ ভরসা হিসেবে চেষ্টা করছেন অনলাইনে কিছু একটা উপার্জন করতে অথবা ফ্রিল্যান্স করতে। ব্যাপারটা কেমন একটা হাস্যকর বিষয় হয়ে যায় অর্থাৎ আপনি সর্বশেষ সব কিছুতে যখন ফেইল করছেন তখনই আসলে অনলাইনকে ক্যারিয়ার হিসেবে বেঁছে নিতে আসছেন।


Spread the love