Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    perl - cgi script to read file from network

    Hi,

    I have written a cgi perl script to read a file and display its contents. But if i have to access file located on network (local network), then i am unable to do so. However if i run script through shell it works, but through cgi script it generates no result. I have mapped network drive even.

    Any suggestions on how to do this.


    Code:

    print "Content-type: text/html\r\n\r\n";
    open (MYFILE, 'U:/abc.txt')
    while (<MYFILE>) {
    chomp;
    print "$_\n \n";
    }
    close (MYFILE);


    U: is the mapped drive

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You should always check the return code of an open call to make sure it was successful and take proper action if it wasn't.

    While debugging cgi scripts, you should use the CGI::Carp module.

    It is better to use a lexical var for the filehandle and the 3 arg form of open.

    Why are you using chomp to remove the \n and then immediately add it back in the print statement?

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI qw(:standard);
    use CGI::Carp qw(fatalsToBrowser);
    
    print header();
    
    open my $fh, '<', 'U:/abc.txt' or die "failed to open 'U:/abc.txt' $!";
    
    while ( <$fh> ) {
        print "$_\n";
    }

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for reply but this is not working. I have observed that if i try to open any file stored locally on my system then it works fine.

    But for file stored on network it fails to open that file. Is there any special handling for accessing files from network.

  • #4
    New Coder
    Join Date
    Nov 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    you're using a mapped network drive, what if you used the UNC path?

    edit: nvm, i see this was your first approach, followed by mapping as a bi-pass.

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Posts
    368
    Thanks
    10
    Thanked 4 Times in 4 Posts
    What is the output from the script. Is it a blank page, or an error? Are you using CGI::Carp to diagnose the problem?

  • #6
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I did some investigation and found it reads file from network if i use forward slash.

    eg if i am having a file 'abc.txt' placed in share folder of pc-hostname, then i can read it by using

    open my $fh, '<', '//pc-hostname/abc.txt' or die "failed to open 'U:/abc.txt' $!";


    But still i am having problem reading file from unix mapped server, i am using CGI::Carp to diagnose problem.

    Now consider if \\sambaserver is unix mapped server and if i try to access same file, i get following error.

    failed to open 'U:/abc.txt' Invalid argument at line 10.

    Here line 10 is line of code in which we are opening file.

    Can anyone please help me in this. What is invalid here ? If i run same perl script through shell, it works fine but on running it through browser is giving me these results.

    Do i need some setting in Apache Config file?

    Thanks
    sarbjit
    Last edited by sarbjit; 08-20-2010 at 10:00 AM.


  •  

    Posting Permissions

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