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
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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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 />";   

}

}

 

?>

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.