Reputation: 11702
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.
Upvotes: 2
Views: 8787
Reputation: 253308
<?php
$directory = "/jobops";
$contents = scandir($directory);
if ($contents) {
foreach($contents as $key => $value) {
if ($value == "." || $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]
.
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/h/i/s/h/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.
Upvotes: 5
Reputation: 11190
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())
);
}
}
Upvotes: 3
Reputation: 47585
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)
Upvotes: 2
Reputation: 11702
<?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
Upvotes: 0