Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a file from server for form

Status
Not open for further replies.

blanius

Programmer
Mar 3, 2002
30
US
I want to allow a user to browse a directory and select a file that then is put into a textfield, Any ideas on an approach to this in PHP?

Bret Lanius
 
use opendir to open the directory. check out the manual for good methods of traversing it.

output each filename as a urlencoded argument to a link. ie
Code:
<a href="<?php echo $_SERVER['PHP_SELF'].'&filename='.urlencode($dir.$file); ?>"><?php echo $file; ?>><br/>

then in your receiving script get the file contents using file_get_contents() and echo into a textfield.


 
I guess I wasn't that clear. I can figure out how to display a list of dir but what I want to do is to get allow the user to select a filename and the filename is put into the textfield.

I suppose this might end up being more of a javascript question.

Bret Lanius
 
You can do it using only PHP although I think what you want is to have the name immediately displayed in the text box upon being selected in the list box in which case it would be a Javascript thing.

Code:
<form action="filebutton.php" method=POST>
<select name="filething" size=5>
<?PHP
$dir="path\to\dir";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
               echo "<option value='$file'>" . $file . "</option>";
        }
        closedir($dh);
    }
}
/*foreach (glob("*.php") as $filename) {
    echo "<option value='$filename'>" . $filename . "</option>";
}*/


?>
</select>
<input type="textbox" name="filename" value="<?PHP if(isset($_POST['filething'])){echo $_POST['filething'];} else{echo "...";} ?>">
<input type=submit value="Put file into textbox">
</form>

for the Js solution something like this should work:

Code:
<select name="myselect" size=5 Onchange="document.forms[0].[red]textboxname[/red].value=this.value;">

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top