Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to find a file using perl

by ITmajor (Beadle)
on Jun 27, 2008 at 21:40 UTC ( [id://694458]=perlquestion: print w/replies, xml ) Need Help??

ITmajor has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a webpage that will allow users to view data from a text file. The text files are named using this format: yymmdd_center.txt What cgi/function will allow me accept input from the user and find & display the text file based on the selection?

Replies are listed 'Best First'.
Re: How to find a file using perl
by runrig (Abbot) on Jun 27, 2008 at 21:50 UTC
    You can accept user input using CGI's param method (after creating some sort of html form of course). You can find the file using glob or opendir/readdir/closedir or File::Find, and you can display the file perhaps using open to open the file, read or readline to read the file, and print to display the file.

    You should also look at taint in perlsec, since there are security issues with accepting user input for use in system commands like open.

    If you need more help, you should probably write a bit of code and ask for help with specific things you are having trouble with.

Re: How to find a file using perl
by pc88mxer (Vicar) on Jun 27, 2008 at 22:30 UTC
    Do you realize that the problem as you have described it has a very simple solution: just put the files in a directory and have your web server serve them directly. The server will list the files automatically when the url corresponding to the directory is accessed.

      There could be security issues in the method you proposed.

      I had a similar problem having to serve data files to the client in an authenticated session. If the files are free to download, an unauthorized user could get them guessing the filenames (and randomize filenames was not an option, because "regular" users wanted meaningful names).

      In the end I used a Perl cgi script that serves the file provided that you ask it with name and md5 hash of its content. So in the authenticated pages I have a link containing both of them, but no one can guess the hash based on a (guessed) filename.

      Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: How to find a file using perl
by psini (Deacon) on Jun 27, 2008 at 21:46 UTC

    The first question should be: "how the user select the file to be displayed?".

    Assuming you provide a method for the user to select the file, your program open the file, read it line by line and send it to the browser.

    Alternatively, I didn't understand your question :)

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: How to find a file using perl
by Popcorn Dave (Abbot) on Jun 28, 2008 at 19:54 UTC
    pc88mxer is right on track. If you're serving up static data files, why not just use a drop down list or radio buttons depending on how many files you have? If it's a dynamic list you could easily build the drop down using Perl.


    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://694458]
Approved by psini
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-19 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found