Jump to content

Scan Directory - How to Use?


MarkT

Recommended Posts

Hi,

How do I use the PHP command scandir

I want to scan the directory and then display them in a table,

 

If anyone could explain it in a way that enables it to be displayed similar to an SQL query return, so $query['filename'] or similar so that I can put it into a table with other details.

 

Thank

Link to comment
Share on other sites

Array(    [0] => .    [1] => ..    [2] => bar.php    [3] => foo.txt    [4] => somedir)

Thats what PHP.net says the result is.

But I don't know how to go about fetching the results in the format I want.

 

Heres what I want it to be similar to

<table class="table table-bordered table-striped table-highlight">						        <thead>						          <tr>						            <th>#</th>						            <th>Date</th>						            <th>Title</th>						            <th>Opponent</th>									<th>Score</th>									<?PHP if($de2['team'] == $_SESSION['team']) {echo "<th>Actions</th>";} ?>						          </tr>						        </thead>						        <tbody>								<?PHP while($se2 = mysqli_fetch_array($search)) {								echo "						          <tr>						            <td>" . $se2['id'] . "</td>						            <td>" . $se2['date'] . "</td>						            <td>" . $se2['title'] . "</td>						            <td>" . $se2['against'] . "</td>						            <td>" . $se2['score'] . "</td>";									if($de2['team'] == $_SESSION['team']) {echo "						            <td><a href='edit-report.php?pid=" . $se2['id'] . "' class='btn btn-small btn-tertiary'>										<i class='btn-icon-only icon-edit'></i>																			</a>																		<a href='manage-reports.php?action=dele&pid=" . $se2['id'] . "&team=" . $se2['team'] . "' class='btn btn-small'>										<i class='btn-icon-only icon-remove'></i>																			</a></td>";}									echo "						          </tr> ";} ?>						        </tbody>						      </table>			

Thats what I use at the moment, with the result being $se2['field']

But I don't know how to get the array in that format so that I can use a similar table.

 

Thanks

Link to comment
Share on other sites

Ditch scandir and use the SPL. Use a DirectoryIterator instead:http://www.php.net/manual/en/class.directoryiterator.phpNote it is only an iterator. If you want the results to be sorted then you'll need to put them all in an array, and then sort them. Like the name implies, DirectoryIterator has the iterator interface, so you can use it in a foreach loop like an array, check the example:http://www.php.net/manual/en/directoryiterator.construct.phpEach item that it iterates through is a SplFileInfo object, which you can see here:http://www.php.net/manual/en/class.splfileinfo.php

Link to comment
Share on other sites

I think I would prefer to use scandir, it looks simpler and more what I would normally use.

 

Could you give me an example of the code that I could implement into my table code shown above.

 

Thanks

Link to comment
Share on other sites

I think I would prefer to use scandir, it looks simpler and more what I would normally use.

Well, ok then. Scandir returns an array of filenames, that's the only thing it will return. It sounds like you don't want to use that though. A DirectoryIterator will give SplFileInfo objects instead of just a string filename. Obviously you can do more with an object than you can with a string. The SplFileInfo class wraps most of the filesystem functions that you would use with individual files, as you can see in the manual. Much more flexible than returning an array of filenames, and it sounds like you're looking for flexibility. If you want an array of filenames though, then you can use the rest of the filesystem functions to get information about each file.http://www.php.net/manual/en/book.filesystem.php
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...