Using CAT command in Perl-CGI script

Discussion in 'Perl' started by upgundecha, Mar 2, 2007.

  1. upgundecha

    upgundecha New Member

    Joined:
    Mar 2, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    How I can cat a file's contents through Perl-CGI. I want to write a Perl-CGI program which will accept file's name from a browser on Windows machine and then cat the contents of that file back to browser window. Any thoughts how to do this?

    Thanks in Advance,
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Here's the code

    Code:
    #!/usr/bin/perl
     
     use CGI;
     
     my $q = new CGI;
     
     $file = $q->param('file') || $ARGV[0];
     
     if(-f $file)
     {
         $txt = qx(cat $file);
     }
     else
     {
         $txt = "File doesn't not exist!";
     }
     
     print $q->header,$txt;
     
  3. upgundecha

    upgundecha New Member

    Joined:
    Mar 2, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey thanks Pradeep,

    But How I can take file name in IE on Windows? And Do I need to give this CGI script root access?

    Regards,
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Just like you do it in *nix, just that you need to have drive names in the beginning of the path/to/file.

    Well you needn't give the script root access, if you are running it from a Web Server apache needs to have read permissions to the file.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice