Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

View Directory on website using php

Posted on 2009-12-17
6
Medium Priority
?
302 Views
Last Modified: 2012-05-08
I have setup a function for my client to upload files via a php form to a specific directory.

What i need to do is display the directory files on a webpage so my client can see whats been uploaded. Also, if this is possible is it then possible to delete files? (not so important but would be useful)

Thanks
0
Comment
Question by:BrighteyesDesign
6 Comments
 
LVL 15

Accepted Solution

by:
ludofulop earned 2000 total points
ID: 26070378
try glob() function:
http://sk.php.net/glob

<?php
foreach (glob("directory/*.*") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>
0
 
LVL 15

Expert Comment

by:ludofulop
ID: 26070380
for deleting files, use unlink() of course:

http://sk.php.net/manual/en/function.unlink.php
0
 
LVL 3

Expert Comment

by:jagdishshirsat
ID: 26070397
hi,

Use this,



<?php
$link = "documents";
if(is_link($link)) {
      $dir = readlink($link);

      if($dh = opendir($dir)) {
            while(($file = readdir($dh)) !== false) {
                  echo "filename: $file -- filetype ".filetype($dir.$file)."\n";
            }
            closedir($dh);
      }
}
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 26070429
try this,

ref. http://php.net/manual/en/function.readdir.php

<?php
// Note that !== did not exist until 4.0.0-RC2

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>

Open in new window

0
 

Author Comment

by:BrighteyesDesign
ID: 26072305
Thanks all!

ludofulop, I have used that script and the directory contents show fine. They show as a list though, what would i add to the code the have each filename appear on a new line?

thanks
0
 
LVL 15

Expert Comment

by:ludofulop
ID: 26072357
just replace \n with br in echo command:

echo "filename: $file -- filetype ".filetype($dir.$file)."<br />"
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question