Jump to content

Display folders and files


rbvinc

Recommended Posts

I need some help please. I am looking for sample code,

or links to do this. I want to display folders, and files in each

folder. I want to put 'href" for each file. These are pdf files.

Thank you for the help in advance.

 

Folder_A,

<a href='a.pdf'>a.pdf</a>

<a href='b.pdf'>b.pdf</a>

<a href='c.pdf'>c.pdf</a>

 

Folder_B

<a href='aa.pdf'>aa.pdf</a>

<a href='bb.pdf'>bb.pdf</a>

<a href='cc.pdf'>cc.pdf</a>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/187751-display-folders-and-files/
Share on other sites

You will want to use glob:

 

foreach (glob("/path/to/folders/", GLOB_ONLYDIR) as $dir) {
    $folder = explode("/", $dir);
    $folder = $folder[count($folder)-1];
    echo $folder . "<br />";

    foreach (glob($dir . "/*.pdf", $file)) {
        $file = basename($file);
        echo "<a href='$file'>$file</a><br />";
    }
}

 

Is one way to do what you want.

Premiso,

Thank you for the reply. I am not familiar with php blobs.

Help me. I could not follow. I did the following code,

and my pdf files are under,

C:\Inetpub\wwwroot\newsa\testpdf

 

Error on line #8,

foreach (glob($dir . "/*.pdf", $file))

 

 

<?php

foreach (glob("/newsa/testpdf/", GLOB_ONLYDIR) as $dir)

{   

$folder = explode("/", $dir);   

$folder = $folder[count($folder)-1];   

echo $folder . "<br />";   

 

foreach (glob($dir . "/*.pdf", $file))

{     

$file = basename($file);       

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

}

}

 

?>

 

 

 

 

Thank you.

 

 

 

Sorry to bother,

I ran it, blank screen showing when I ran it, thought

I have two pdf files in that folder.

C:\Inetpub\wwwroot\newsa\testpdf

 

Error on this new line code,

foreach (glob($dir . "/*.pdf") as $file)

 

Please note, I have php version, PHP Version 4.3.11

 

Thank you,

Full code here,

 

 

<?php

 

foreach (glob("/newsa/testpdf/", GLOB_ONLYDIR) as $dir)

{   

$folder = explode("/", $dir);   

$folder = $folder[count($folder)-1];   

echo $folder . "<br />";   

 

foreach (glob($dir . "/*.pdf") as $file)

 

{     

$file = basename($file);       

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

}

}

 

?>

 

Add this to your script:

 

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

foreach (glob("/newsa/testpdf/", GLOB_ONLYDIR) as $dir)
{   
    $folder = explode("/", $dir);   
    $folder = $folder[count($folder)-1];   
    echo $folder . "<br />";   

    foreach (glob($dir . "/*.pdf") as $file)
    {       
        $file = basename($file);       
        echo "<a href='$file'>$file</a><br />";   
    }
}

?>

 

See what the error says (if any).

Chances are the directory is wrong or empty. What is the full path to the directory from the root directory, try using that.

 

Basically what that means is that it never enters the foreach loop, which could be the above, no folders or wrong directory.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.