The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> search directory for a file, search directory for a file
antamr904
post May 4 2012, 07:54 AM
Post #1





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



hi all,

i am very new to html and any web scripting lang so please be patient with me.

what i am trying to accomplish is:

i have a basic html page with a text box and a search button.

i would like for the user to type a computer name in the text box field then hit search

the search button would:

search a local directory called (inv) for a txt file named %computername%.txt on the web server then display the contents of that txt file in a iframe.

again i am very new to any type of web scripting language, please let me know if this can be accomplished any other way.

thank you in advance!!

here is what i have so far:

<html>
<body>
<center>
<h1>Computer Inventory</h1>
</center>
<br>
<br>
<br>
<h3>Search</h3>
Computer Name:
<br>
<input type="text" name="SearchInv" size="50">
<input id=runbutton class="button" type="button" value="Find" name="run_button" onClick="RunScript">
<br>
<h3>Here is the computers inventory:</h3>
<iframe src="inv/0171afaugno.txt" width="1000" height="500" align="left">



</body>

</html>


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post May 4 2012, 08:05 AM
Post #2


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



A site can't actively search the user's hard drive. If you mean a Browse button, it does just that, lets the use browse his own drive for files to upload. It's created with <input type="file"> .
http://htmlhelp.com/reference/html40/forms/input.html
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 4 2012, 08:09 AM
Post #3





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



QUOTE(pandy @ May 4 2012, 09:05 AM) *

A site can't actively search the user's hard drive. If you mean a Browse button, it does just that, lets the use browse his own drive for files to upload. It's created with <input type="file"> .
http://htmlhelp.com/reference/html40/forms/input.html


thank you for your reply.

i don't want to search the users hd, i want to search a local directory called (inv) on the iis server then display the contents of the txt file in a iframe.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post May 4 2012, 12:11 PM
Post #4


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Sorry, I misunderstood.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 4 2012, 01:59 PM
Post #5





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



any help will be greatly appreciated.

basically on my web server there is a folder called (inv) with a bunch of txt files named (%computername%.txt

i would like to create a text search box so all i have to do is type the computer name in the text box > hit search > and the content of that txt file is displayed in a iframe.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 4 2012, 06:32 PM
Post #6


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



Which server-side scripting languages can you use?

Assuming that the user has submitted a real HTML form, you might use PHP http://php.net/manual/en/function.file-exists.php to check if the searched file exists, and then http://php.net/manual/en/function.file-get-contents.php to read its content and print on the form result page (or in a framed page, though I don't see the point with that).

Note that when searching for a computer name, any typo may affect the search result. Also the user must spend time typing. Why not list all the files in the directory (say as a list of links) instead? This can be done with e.g. http://php.net/manual/en/function.scandir.php
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post May 4 2012, 10:22 PM
Post #7


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



In php there is an instruction called file_exists( 'filename.and.type' ) which just returns a true or false if the 'fliename.and.type' file exists.

This can be used to implement a search for the desired file.

if( file_exists( '/path/to/inv/file2.txt' ) )
{
perform whatever;
}

This post has been edited by Ephraim F. Moya: May 4 2012, 10:26 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 8 2012, 10:05 AM
Post #8





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



thank you all for your response, but how would i get the computer name that the user entered as a variable and add that to the search function??

again i am very new to any type of web development.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 8 2012, 01:55 PM
Post #9


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



See http://www.php.net/manual/en/tutorial.forms.php
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 8 2012, 02:38 PM
Post #10





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



thanks again all,

one more question

with the - if (file_exists) how can i redirect to anther page if the file the user typed in DOES NOT exist?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 8 2012, 05:02 PM
Post #11


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



You can send a redirect with the PHP header() function.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post May 9 2012, 05:32 PM
Post #12


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Christian J @ May 8 2012, 04:02 PM) *

You can send a redirect with the PHP header() function.


No Need! Just send an ack page or a try again page. So:

if file_exists( 'filename.txt' )
{
sendpage( 'AckPage' ); // File is there
}
else
{
sendpage( 'TryAgainPage' ); // file is NOT there
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 10 2012, 10:12 AM
Post #13





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



great i will try out both options, TY!!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 10 2012, 11:34 AM
Post #14





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



hi all,

how would i perform more then one function if file_exists??

like get contents and print?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
antamr904
post May 10 2012, 02:51 PM
Post #15





Group: Members
Posts: 8
Joined: 4-May 12
Member No.: 17,049



ok im stuck,

i want to check if the file name exist and if it does display a message AND print the contents of the file AND if the file DOES NOT exist display a message.

here is my code:

<?PHP

$compname = $_POST['compname'];

if (file_exists($compname)) {
echo "NOTE: $compname exists in our inventory";
} elseif (file_exists($compname)) {
(file_get_contents('$compname'));
echo $compname;
} else {
echo "NOTE: $compname does NOT exists in our inventory";
}

?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post May 10 2012, 09:49 PM
Post #16


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(antamr904 @ May 10 2012, 01:51 PM) *

ok im stuck,

i want to check if the file name exist and if it does display a message AND print the contents of the file AND if the file DOES NOT exist display a message.

here is my code:

<?PHP

$compname = $_POST['compname'];

if (file_exists($compname)) {
echo "NOTE: $compname exists in our inventory";
} elseif (file_exists($compname)) {
(file_get_contents('$compname'));
echo $compname;
} else {
echo "NOTE: $compname does NOT exists in our inventory";
}

?>


file_exists() is a BINARY instruction. That means that the first answer you get is the answer. And it's TRUE or FALSE. Doing it again will give you the same answer: The same TRUE or FALSE.

If you're somehow getting three answers then you're doing something wrong.

So your problem should be solved like this:

if ( file_exists( $compname ))
{
echo "\nNOTE: $compname exists in our inventory\n";
$contents = file_get_contents( $compname );
echo "<br><br>" . $contents;
}
else
{
echo "NOTE: File $compname does NOT exist in our inventory";
}

ps. Look for and read a basic php programming tutorial. Then read the whole thing over and over until it starts to make sense.

This post has been edited by Ephraim F. Moya: May 10 2012, 10:04 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 03:55 PM