Find the answer to your Linux question:
Results 1 to 6 of 6
hello. can someone help me please? i need to make a php script to display a files on my server in browser. i m trying a couple of days but ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    26

    need help with php


    hello.

    can someone help me please? i need to make a php script to display a files on my server in browser.
    i m trying a couple of days but i can't do this

    let me bee more specific.

    i need a form with text field and submit button, so i can enter some directory and when i click on submit button
    on next page to display the contents of thet directory. for example when i enter /home in text field on next
    page to see all files and folders in that directory.

    i have a submit form like the one bellow:
    <HTML>
    <HEAD>
    <TITLE>Display Directory Contents</TITLE>
    </HEAD>
    <BODY>




    Enter Directory Name:

    <INPUT type="text" NAME="directory_name" SIZE=30></P>


    <INPUT TYPE="submit" NAME="submit" VALUE="Display"></P>
    </FORM>
    </BODY>
    </HTML>

    and :

    <?

    $dir_name = "/home/";

    $dir = opendir($dir_name);

    $file_list = "<ul>";

    while ($file_name = readdir($dir)) {

    if (($file_name != ".") && ($file_name != "..")) {
    $file_list .= "[*]$file_name";

    }
    }

    $file_list .= "[/list]";

    closedir($dir);

    ?>
    <HTML>
    <HEAD>
    <TITLE>Directory Listing</TITLE>
    </HEAD>
    <BODY>



    Files in: <? echo "$dir_name"; ?></P>

    <? echo "$file_list"; ?>
    </BODY>
    </HTML>

    the second php code is workin fine. it display all files in home directory, but i need to be able to display
    directory that i want to be displayed.

    example when i enter in first page /root to display all files
    in /root directory, or when i enter /usr to display all files in /usr directory.

    thanks in advance

  2. #2
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    Hi ice9

    You may want to check what scandir does (http://www.php.net/manual/en/function.scandir.php), but PHPv5 is required, else your suggestion of opening, reading and closing is your best bet.

    Your form will need to have an action and a method. In this case metjod=POST will do to read out the value of what the user submitted. Have a read of this: http://www.w3schools.com/php/php_forms.asp.

    You will probably find examples of what you are looking for when googling for "scandir".

    Hope this helps


    Tech

  3. #3
    Linux User Game master pro's Avatar
    Join Date
    Sep 2005
    Location
    Tasmania, australia
    Posts
    274
    I see what the problem is i think...

    <INPUT type="text" NAME="directory_name" SIZE=30></P>

    that is of type "text" and name of "directory_name"
    i suggest you change it to type "post"

    <INPUT type="post" NAME="directory_name" SIZE=30></P>

    so.

    $dir_name = $_POST['directory_name'];


    doing that will set $dir_name to what you typed in the box named directory_name

    Hope this helped

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jul 2004
    Posts
    94
    Um, POST is a method, not a type. If the input is of type text, it will show up in the associative array $_POST under the key that is the name of the input element.

    In other words, the original statement of the thread starter is syntactically correct.

  6. #5
    Just Joined!
    Join Date
    Aug 2005
    Posts
    26
    ok. i writh this script and its work fine. here is the script so someone can use it too. but i have one problem. i can't see the contents of /root directory. how can i do this but not to chage privilegies for /root folder on the server.

    thanks in advance. here is the script

    <HTML>
    <HEAD>
    <TITLE>Display Directory Contents</TITLE>
    </HEAD>
    <BODY>

    <form action="backend.php" method="post">


    Enter Directory Name:

    <INPUT type="text" NAME="directory_name" SIZE=30></P>


    <INPUT TYPE="submit" NAME="submit" VALUE="Display"></P>
    </FORM>
    </BODY>
    </HTML>


    <?
    if ($directory_name != "") {
    } else {
    $directory_name = ( isset($HTTP_GET_VARS['directory_name']) ) ? $HTTP_GET_VARS['directory_name'] : $HTTP_POST_VARS['directory_name'];
    $directory_name = htmlspecialchars($directory_name);
    }


    $list = split("\n",`ls $directory_name`);

    $pattern = "/[dwrx\-]{10}/";

    foreach($list as $file)
    {
    $file = preg_split("/ /",$file,20,PREG_SPLIT_NO_EMPTY);
    echo "<a href='backend.php?directory_name=$directory_name/";
    echo join("|",$file) ."'>\n";
    echo join("|",$file) ."
    \n";
    print "</a>";
    //echo "$directory_name";
    //echo "$file[0]\n";
    }
    ?>

  7. #6
    Linux User Game master pro's Avatar
    Join Date
    Sep 2005
    Location
    Tasmania, australia
    Posts
    274
    Quote Originally Posted by Reisswolf
    Um, POST is a method, not a type. If the input is of type text, it will show up in the associative array $_POST under the key that is the name of the input element.

    In other words, the original statement of the thread starter is syntactically correct.
    Yes, you're correct sorry, i was like, half asleep when i wrote that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •