Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Why can I not acess files outside cgi-bin directory?

by perlfeng (Initiate)
on Mar 24, 2005 at 20:28 UTC ( #442187=perlquestion: print w/replies, xml ) Need Help??

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

Hi, there, I am new to cgi and may ask a stupid question. I am working on a problem of downloading a file to client through browser. My cgi script is supposed to process the user requests and download the file in the server to the client machine. A popup window will let user to select where the file will be saved. If the target file is in cgi-bin directory where my cgi script is running (e.g. test1.txt). But I can not acess the file outside this directory (e.g. text2.txt).

$filename1 = "test1.txt";

$filename2 = "C:\\Program Files\\Apache Group\\Apache2\\htdocs\\projects\\Generate_Conc_xls\\test2.txt";

Any advices?

Thanks a lot.

  • Comment on Why can I not acess files outside cgi-bin directory?

Replies are listed 'Best First'.
Re: Why I can not acess files outside cgi-bin directory?
by ww (Archbishop) on Mar 24, 2005 at 20:54 UTC
    Based on the information supplied, I can only offer some suspicions:
    Server configuration?
    OS? are you truly running your server on windoze, or -- as I did once -- are you testing on an MS box and then being befuddled when *n*x did not like what I'd done?
    or might you have a code issue?
    ... in which case, posting some code would help us help you (though the double backslashes look vaguely questionable).

    Welcome... Hope someone can help based on what you've given us, but please check out the various FAQs and guides, for how to get better help on further posts.

      Hi, thank you schodckwm, here is my testing code. I used active perl 5.81 on windows xp. It seems that the I need use \\ to represent the '\' on windows to represent directory delimiter. The code works if I use $filename = "test.txt";.
      #!c:/perl/bin/perl -w use CGI; # only files in cgi-bin directory works? #filename = "C:\\Program Files\\Apache Group\\Apache2\\htdocs\\project +s\\Generate_Conc_xls\\test.txt"; $filename = "test.txt"; $header1 = "Content-disposition: attachment; filename=$filename\n"; $mime_type = "application/octet-stream"; #print header for downloading file to client print $header1; print "Content-Type: $mime_type\n\n"; #print content to client file open (READ, "<$filename"); binmode READ; local $/; print <READ>; close(READ);
      Thanks a lot.

        Change your code to test that your open succeeds:

        open( READ, "<$filename" ) or die "Failed to open $filename - error $! +";

        That might provide some clues in your webservers error log.

        Steve
        ---
        steve.org.uk
Re: Why can I not acess files outside cgi-bin directory?
by jhourcle (Prior) on Mar 25, 2005 at 01:16 UTC

    Is the webserver CHROOTed?

    If it is, then the path to access the file (if you can at all), would not be the same as it would on the hard drive.

    I'm not sure what the implications are with CHROOT and windows, or if it's even possible, not being a windows user (well, for games, just not for servers).

    I'm going to do with the basic rule of debugging CGI scripts -- read your error log. If you don't have access to the error log, for whatever reason, bug your admin.

      Thanks a lot for your guys. The suggestions are all helpful. Now I have cleared the following issues:

      1. It is not because of the server since the server did not give the errors in error log.

      2. The script itself works fine without error or warning when it is running. Double backslash is required if put the file path in "" on windows. I will test if it is ok if put in '' and one backslash.

      3. It seems that the problem maybe related to windows OS as suggested by nerfherder. But I have no clue on it now.

      I would pin down the problem to the file format now. It seems that the text file can be read out from outside cgi-bin. I will b back soon if I got it solved.

      Thanks.

        Hi, All,

        I pinned down the problem to the data file itself because I can download other files outside of cgi-bin to the client. I suspected the data file was somehow damaged when I got it. But I still can open in a text editor. And strange enoght, I can open it in a local perl script!

        I also tested that it is not because the data file is too large as nobull suggested. Thanks for gam3 for reminding me that to use file path in '' with one backslash. It is much easy to read and comfortable.

        Thank you all again for giving advices on this question. It is helpful. Really appreciate it!

Re: Why can I not acess files outside cgi-bin directory?
by nerfherder (Monk) on Mar 25, 2005 at 00:09 UTC
    The problem is most likely that while your script is in the cgi-bin directory, and has ExecCGI permissions, the file "C:\\Program Files\\Apache Group\\Apache2\\htdocs\\projects\\Generate_Conc_xls\\test2.txt" is in a directory which does not.

    What you probably need is a .htaccess file in the "C:\\Program Files\\Apache Group\\Apache2\\htdocs\\projects\\Generate_Conc_xls\\" directory. Or perhaps another Directory statement in httpd.conf.

    I'll leave it to you to "google it out" from there, since I suspect this is more Apache/Windows-related than Perl-related. However, since it is windows and you can't just set the permissions on the file and dir in question to that of the webserver owner, the script probably can't access the file during runtime unless it also has ExecCGI.

    Don't forget to check the apache error_log for clues - this will point you in the right direction. Afterthought: maybe it has to do with the backslashes in the path; you may be able to get away with forward slashes -- I've seen it go both ways (wacky windoze!). Good luck!
Re: Why can I not acess files outside cgi-bin directory?
by gam3 (Curate) on Mar 25, 2005 at 01:02 UTC
    Not that it will help your problem, but you might try
    $filename = C:\Program Files\Apache Group\Apache2\htdocs\projects\Generate_Conc_xls\test.txt';
    
    as it is much easier to read.

    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2021-10-19 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (77 votes). Check out past polls.

    Notices?