Friday 28 March 2014

File Handling Listing files in php

File Handling Files in Php programming Language. In File handling we learn open a file write to the file, read to the file and appending to the file. In this we will learn how to look up to the specific directory and list all files. With the help of loop in php the content of the directory display foreach file. We are going to display a list then we create hyperlink foreach file, So we can click on it and go through it. Firstly Why it may Useful. Let Say You were creating some kind of file based Photo gallery and you want to have several folder for example. For example we had Files directory in which we have firstfile.txt, secondfile.txt and thirdfile.txt. Now what we are going to do is we are going to be looking inside files directory listing all files. You may want to create php script search an particular directory list of images display on a page. So, you can adopt this example i am going to use really really easily into your website and for your own purposes.

We are going to be using two different functions open directory and then reading directory You properly guest difference between this two open directory function will actually open the directory load into handle which we had done in file handling in php. With read directory we can put this into loop and create read directory as an another variable. So, the read directory gone be read handle which evaluate with another variable called files. So, You gone understand properly by example.


Code for File Handling Listing Files

File Handling Listing files in php
Screenshot1


<?Php



$directory = 'files';



if ($handle = opendir($directory.'/')) {

	echo 'Looking inside\''.$directory.'\':<br>';



while ($file = readdir($handle)) {

if ($file!='.'&&$file!='..') {

echo '<a href="'.$directory.'/'.$file.'">'.$file.'</a><br>';

}

}

}

?>

Output 

File Operation in php
Screenshot2
File Handling Listing Files in php
Screenshot3
Example for file handling listing files in php. In this example firstly we will create variable called directory. This variable will address to the particular file in which we are looking for the files. 

$directory = 'files';

files is the folder name in which we have files to look inside the directory.

Now we had to create handle to handle function opendir function 

if ($handle = opendir($directory.'/')){

In this we had created function opendir which open the directory and its appended with forward slash. If its find and open the directory which we assign in $directory variable such as files it will echo out

echo 'looking inside\''.$directory.'<br>';

As you can see output in screenshot2 Its open the directory and echo out the text we had looking inside files.

While ($file = readdir($handle)

Now in this code above we had while loop it will read directory and we applying handle into that So, We reading in the $handle that we already open with opendir function. Every Time we go around this loop Variable $file going to equal to next file along. Now here files are in text format for going to the file from browser we have to link to it for that we will use simple html hyperlink.

if ($file!='.'&&$file!='..') Its if condition to remove dots from which assigns to go forward and backward which we now removing from if conditions.

echo '<a href="'.$directory.'/'.$file.'">'.$file.'</a></br>';

This will link to the files to the particular directory called files. 

As you can see screenshot3 the link to the files and directory is opened. By clicking on the file name it will go to the content of the file in this way we can create particular directory to the browser by php script.



No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates