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

Hi everybody. This is my first post here, I hope you can help me (I'm sorry if my english is not good).
I builded a system using CGI. The system is accessed from another computer. For example in http://1.1.1.1/mySystem/modulePrint (ofcourse the ip of the server is not real) there is an option that if a button is clicked then a myfile.txt is printed.
In my modulePrinting.pm I have the following line:

 open DATA, "+>/usr/local/tmp_files/temp_file_for_print.txt" or die "Couldn't open file $!";

Then I add some lines to the file and then I have:
my $cmd = "lpr /usr/local/smot/impresion_codigo.lbl"; qx/$cmd/;

With this I execute the lpr command. Well, my problem is that when I pressed the button I get the followin error:

Couldn't open file, Permission denied at /usr/lib/perl5/5.8.8/CGI/Carp.pm line 314.

I've looked other ways to create the file and give it permissions but I've failed in finding an answer. I tried to open the file in other directory (/home/myUser/ for example) but I get the same result.
Can someone help me please. I'd be very grateful.

Replies are listed 'Best First'.
Re: File Permission with open
by morgon (Priest) on Oct 15, 2010 at 23:16 UTC
    It seems that your open fails and that is probably because the user-id that the CGI-process runs under does not have write permissions to the directory in which you want to create it.

    And btw: As you are not capturing any output you should use system($cmd) rather than qx/$cmd/.

      Thank you for your answer.
      I'm sorry my ignorance, but how can I solve that problem?.
      I need to give write permissions to the computer (that is not the server) that navigates through the system?...or I need to configure some file in my server?. Is there a way that I can create the file and give it all the permissions?.
      Thank you.
        I think the problem lies in directory-permissions on the server.

        Forgive me for saying this but I would really advise you to seek some advice from a sysadmin (if you have one) because you seem be to be not that experienced and could cause a lot of problems if you change permissions on the server and don't really know what you are doing...

        But if you are on your own try to create your temporary files in the /tmp directory which normally is writeable for all.

Re: File Permission with open
by Khen1950fx (Canon) on Oct 16, 2010 at 01:56 UTC
    I tried a different way by opening a process:
    #!/usr/bin/perl use strict; use warnings; my $file = '/usr/local/tmp_files/temp_file_for_print.txt'; open (LPR, "| lpr -P/dev/lp"); print LPR $file; close(LPR);
      Thanks, but if I'm not mistaken, there you only print the file. You assume that the file already exits...right?, But what I need is create the file, add some lines to the files and then print it. Like it has been said before, it's in the "open" part where I'm having the problem.
      Thanks for the answers, but I'm still suffering with this problem.