PHP Help

baseman101

Baseband Member
Messages
26
Location
Purcellville, Virginia
I'm trying to make a file viewer in PHP, but I've run across a problem. It's worked when I used goto;, but I'm not using it because it only works in the later PHP versions. I settled on a function. Basically, the problem is that the $postpath variable doesn't work correctly. It gets defined as nothing. I don't know how this is happening, since I have isset(). Can you help?
PHP:
<?php
echo "<html>\n";
echo "<head>\n";
echo "<title>File Viewer</title>\n";
echo "</head>\n";
echo "<body>\n";
function files()
{
    if (isset($postpath) === false) {
        $postpath = ".";
        global $postpath;
    }
    global $postpath;
    $path = $postpath;
    $dir_handle = @opendir($path) or die("Could not open file $path");
    echo "<form action=\"#\" method=\"post\">\n";
    echo "Directory: " . $path . "<br />\n";
    while ($file = readdir($dir_handle)) {
        if ($file == ".")
            continue;
        echo "<input type=\"radio\" name=\"filename\" value=\"$path/$file\">$file<br />\n";
    }
    echo "<input type=\"submit\" value=\"Go\">\n";
    echo "</form>\n";
    echo "</body>\n";
    echo "</html>";
    closedir($dir_handle);
}
if ($_POST) {
    if (is_dir($_POST['filename'])) {
        $postpath = $_POST['filename'];
        files();
        exit;
    }
    $src = $_POST['filename'];
    show_source($src);
    echo "</body>\n";
    echo "</html>";
    exit;
}
files();
?>
 
Last edited:
That actually works with just a couple of modifications so you've clearly given it a good go but there is a problem in the way that you have used the 'global' keyword.
PHP:
  1 <?php
  2 $postpath = ".";
  3 echo "<html>\n";
  4 echo "<head>\n";
  5 echo "<title>File Viewer</title>\n";
  6 echo "</head>\n";
  7 echo "<body>\n";
  8 function files()
  9 {
 10     global $postpath;
 11     if (isset($postpath) === false) {
 12         $postpath = ".";
 13     }
 14     $path = $postpath;
 15     $dir_handle = opendir($path) or die("Could not open file $path\n");
 16     echo "<form action=\"#\" method=\"post\">\n";
 17     echo "Directory: " . $path . "<br />\n";
 18     while ($file = readdir($dir_handle)) {
 19         if ($file == ".")
 20             continue;
 21         echo "<input type=\"radio\" name=\"filename\" value=\"$path/$file\">$file<br />\n";
 22     }   
 23     echo "<input type=\"submit\" value=\"Go\">\n";
 24     echo "</form>\n";
 25     echo "</body>\n";
 26     echo "</html>";
 27     closedir($dir_handle);
 28 }   
 29 if ($_POST) {
 30     if (is_dir($_POST['filename'])) {
 31         $postpath = $_POST['filename'];
 32         files();
 33         exit;
 34     }   
 35     $src = $_POST['filename'];
 36     show_source($src);
 37     echo "</body>\n";
 38     echo "</html>";
 39     exit;
 40 }   
 41 files();
 42 ?>
 
Alright, thanks for the last help, but I have two more concerns. I've decided to make a little menu that appears when you select a file. The problem is, when I press on a file, it makes the file menu on the right go to root (./). I tried defining the variable for the path but that gave me an error (Cannot open). What can I do to stop this?

And for my second question. I've added a div on the right that lets you execute actions for each file, (I don't have actions as of yet) but it shows you the whole file path (e.g ./bar/foo/../../index.php) which is really annoying. How can I just display the file's name and extension (index.php)?

PHP:
<?php
echo "<html>\n";
echo "<head>\n";
echo "<style type=\"text/css\">\n";
echo "#left {\n";
echo "float: left;\n";
echo "width: 175px;\n";
echo "clear: left;\n";
echo "}\n";
echo "#right {\n";
echo "position: fixed;\n";
echo "left: 200px;\n";
echo "}\n";
echo "</style>\n";
echo "<title>File Viewer</title>\n";
echo "</head>\n";
echo "<body>\n";
function files()
{
    global $postpath;
    if (isset($postpath) === false) {
        $postpath = ".";
    }
    global $postpath;
    $path = $postpath;
    $dir_handle = @opendir($path) or die("Could not open file $path");
    echo "<div id=\"left\">\n";
    echo "<form action=\"#\" method=\"post\">\n";
    while ($file = readdir($dir_handle)) {
        if ($file == "." || $file == "..")
            continue;
        echo "<input type=\"radio\" name=\"filename\" value=\"$path/$file\">$file<br />\n";
    }
    echo "<input type=\"submit\" value=\"Open File Actions\">\n";
    echo "</form>\n";
    echo "</div>\n";
    echo "</body>\n";
    echo "</html>";
    closedir($dir_handle);
}
if ($_POST) {
    if (is_dir($_POST['filename'])) {
        $postpath = $_POST['filename'];
        files();
        exit;
    }
    echo "<div id=\"right\">\n";
    $file_action = $_POST['filename'];
    $postpath = $_POST['filename'];
    echo "File Location: $file_action\n";
    echo "</div>\n";
}
files();
?>
 
#1 verify the file path your are sending into opendir. i assume this needs to be a filesystem path as opposed to a web url

#2 you could split the string using \ (or / if linux) as a separator and then show the upper limit value

PHP: explode - Manual

PHP: end - Manual
Thank you. Explode worked and with end, I was successful. I'm still working on my first problem.

Edit: I fixed the first problem.
 
Last edited:
I remember a third question. Sometimes, the directory is too big, so the list of files go to the bottom of the page. How would I make the list of files stay on the same page, but are next to each other? Here's what I mean.
1. file1.php 6. Foo
2. file2.html
3. file3.css
4. dir1
5. Bar
----------------
End of page
 
Back
Top Bottom