tags:

views:

408

answers:

4

I'm really new to php but what I want to do is make a dynamic table of contents for some of my companies job offerings.

so if i put all the pdfs of the job descriptions in a folder called ../jobops with the php file located in root directory what would i need to do.

+2  A: 
<?php
  $directory = "/jobops";
  $contents = scandir($directory);
    if ($contents) {
       foreach($contents as $key => $value) {
             if ($value == "." || or $value == "..") {
                unset($key);
             }
       }
    }
        echo "<ul>";
    foreach($contents as $k => $v) {
      echo "<li><a href=\"$directory/" . $v . "\">link text</a></li>";
    }
        echo "</ul>";

?>

Should work. Takes the directory, scans it, maps all the filenames into an array $contents removes the relative urls (the "." and "..") and then echoes them out.

Remember that foreach is relatively expensive, though; so you may wish to simply unset $contents[0] and $contents[1].


Edited in response to the following (from the OP):

Warning: scandir(www.markonsolutions.com/jobops) [function.scandir]: failed to open dir: No such file or directory in /home/content/t/i/m/timhish/html/test.php on line 5 Warning: scandir() [function.scandir]: (errno 2): No such file or directory in /home/content/t/i/m/timhish/html/test.php on line 5 Warning: Invalid argument supplied for foreach() in /home/content/t/i/m/timhish/html/test.php on line 15 I changed ti from "/jobops" thinking it was a relative directory thing but apparently that's not it. also im not sure what the /home/content....... thing is but i am currently hosted with go daddy maybe thats how they store things?

The $directory variable is relative to where the script is being called. In my case, this runs from the root folder so it should be, for me, $directory = "jobops" assuming the folder and script are stored in the same place. Without knowing your server's directory structure I can't really help you, but I would suggest ruling out a problem with the scandir() function.

To do this, create a folder in the same directory as your script called whatever you like, populate it with at least one image (so that the following if() doesn't unset the entire array) and see if that works. If it does then you're stuck with finding the relative path to your folder. If it doesn't then I'm stuck myself.

David Thomas
sweet thanks I'm trying it now (ill have to tweak it a little) can you explain more about the unset I don't follow?
Crash893
I get an error on what i believe is the if statement . ..
Crash893
The `unset($key)` unsets the variables (`$content[0]` and `$content[1]`. If you omit the `unset()` step you'll see what it removes, and why (I think) you should.
David Thomas
@Crash, what's the error?
David Thomas
Warning: scandir(www.markonsolutions.com/jobops) [function.scandir]: failed to open dir: No such file or directory in /home/content/t/i/m/timhish/html/test.php on line 5Warning: scandir() [function.scandir]: (errno 2): No such file or directory in /home/content/t/i/m/timhish/html/test.php on line 5 Warning: Invalid argument supplied for foreach() in /home/content/t/i/m/timhish/html/test.php on line 15
Crash893
I changed ti from "/jobops" thinking it was a relative direcotry thing but apprently thats not it. also im not sure what the /home/content....... thing is but i am currently hosted with go daddy maybe thats how they store things?
Crash893
A: 
<?php
  // open this directory 
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
     print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
     print("<td>");
     print(filetype($dirArray[$index]));
     print("</td>");
     print("<td>");
     print(filesize($dirArray[$index]));
     print("</td>");
     print("</TR>\n");
    }
}
print("</TABLE>\n");


?>

seems to be working but I need to tweak it to only search a sub dir and only *.pdf files

Crash893
+2  A: 

take a look at fnmatch() or glob() functions.

(i'd have liked to post it as a comment, but the link would have been too long)

Boris Guéry
I never even *thought* of `glob()`...
David Thomas
i read in your link that glob can replace opendir but how would it knwo which dircotry to point at?
Crash893
+1  A: 

The SPL (PHP5) provides a nice interface to directory traversal:

// whitelist of valid file types (extension => display name)
$valid = array(
    'pdf' => 'PDF',
    'doc' => 'Word'
);

$files = array();    

$dir = new DirectoryIterator($directory_path);

foreach($dir as $file)
{
    // filter out directories
    if($file->isDot() || !$file->isFile()) continue;

    // Use pathinfo to get the file extension
    $info = pathinfo($file->getPathname());

    // Check there is an extension and it is in the whitelist
    if(isset($info['extension']) && isset($valid[$info['extension']])) 
    {
        $files[] = array(
            'filename' => $file->getFilename(),
            'size' => $file->getSize(),
            'type' => $valid[$info['extension']], // 'PDF' or 'Word'
            'created' => date('Y-m-d H:i:s', $file->getCTime())
        );
    }
}
rojoca
Parse error: syntax error, unexpected '{' in /home/content/t/i/m/timhish/html/test.php on line 24
Crash893
Just needed an extra bracket on that second if statement
rojoca