Hi. I am having trouble finding an answer to what I think may be pretty easy. I’ve been on several forums, but they are all giving me answers like I know a lot about PHP, which I don’t. I can’t even find an example other than the one below. I just can’t figure out how to “open” the pdf from the submit button. I am never going to have enough files for database and right now I am not concerned about security.
Anyway, I would like a drop down menu that reads from folders on my server. Say they choose cars, the next dropdown would generate all the pdfs available to download. Once they choose from the dropdown, it will open the pdf.
Here is what I have so far, which works, I just don’t know how to force download from submit:
[php]<?php
$parent_directory = ‘C:\inetpub\wwwroot\Test1’;
$file_types = ‘pdf’;
//===================================================//
// FUNCTION: directoryToArray //
// //
// Parameters: //
// - $root: The directory to process //
// - $to_return: f=files, d=directories, b=both //
// - $file_types: the extensions of file types to //
// to return if files selected //
//===================================================//
function directoryToArray($root, $to_return=‘b’, $file_types=false) {
$array_items = array();
if ($file_types) { $file_types=explode(’,’,$file_types); }
if ($handle = opendir($root)) {
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “…”) {
$add_item = false;
$type = (is_dir($root. "/" . $file))?'d':'f';
$name = preg_replace("/\/\//si", "/", $file);
if ($type=='d' && ($to_return=='b' || $to_return=='d') ) {
$add_item = true;
}
if ($type=='f' && ($to_return=='b' || $to_return=='f') ) {
$ext = end(explode('.',$name));
if ( !$file_types || in_array($ext, $file_types) ) {
$add_item = true;
}
}
if ($add_item) {
$array_items[] = array ( 'name'=>$name, 'type'=>$type, 'root'=>$root);
}
}
} // End While
closedir($handle);
} // End If
return $array_items;
}
if (isset($_POST[pickfile])) {
// User has selected a file take whatever action you want based
// upon the values for folder and file
} else {
echo '
';
echo “<form name=“pickFile” method=“POST”>\n”;
$directoryList = directoryToArray($parent_directory,‘d’);
echo “<select name=“folder” onchange=“changeFolder(this.value);”>\n”;
foreach ($directoryList as $folder) {
$selected = ($_POST[folder]==$folder[name])? ‘selected’ : ‘’;
echo “<option value=”$folder[name]" $selected>$folder[name]\n";
}
echo ‘
’;
$working_folder = ($_POST[folder]) ? $_POST[folder] : $directoryList[0][name];
$fileList = directoryToArray($parent_directory.’/’.$working_folder,‘f’,$file_types);
echo “<select name=“file”>\n”;
foreach ($fileList as $file) {
echo “<option value=”$file[name]">$file[name]\n";
}
echo ‘
’;
echo “<button type=“submit” name=“pickfile”>Submit\n”;
echo “\n”;
echo “\n”;
echo “\n”;
}
?>[/php]
Thanks for any help in solving this.