Solved

Open File for Write

Posted on 1998-04-11
11
186 Views
Last Modified: 2013-12-25
I have a Perl CGI program which is supposed to open a file for writing. If I run the program from the Telnet command line, it works okay. It opens the file and the program can write to it. However, when it is run as the result of a POST method from a FORM in an HTML page, it can't open the file; it returns a message saying it can't open the file. If the file already exists, it can open it, but in rreal life it can't already exist because it needs to open a new file every time it is invoked. So it generates a new filename each time and tries to open it.

Here is a sample program (not the real CGI program) that exhibits the behavior described above:

#!/usr/local/bin/perl5.003
require 'cgi-lib.pl';
$neworder = '/web/guide/imiinc/orders/f.htm';
open(ORDER,">$neworder") || &CgiDie("Can't open $neworder");
print ORDER "ANYTHING\n";
close(ORDER);  
0
Comment
Question by:cbutton
  • 4
  • 4
  • 3
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1832265
What does it say if you do
&CgiDie("Can't open $neworder because $!");
0
 
LVL 84

Expert Comment

by:ozo
ID: 1832266
What are the permissions/owner of the /web/guide/imiinc/orders directory?
0
 
LVL 5

Accepted Solution

by:
julio011597 earned 170 total points
ID: 1832267
Sounds like a permission problem, so please try:

$ chmod 777 target_dir

where <target_dir> is the directory where you want to create the file.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Expert Comment

by:julio011597
ID: 1832268
Sorry ozo, i had this open before you put your first comment :(
0
 

Author Comment

by:cbutton
ID: 1832269
julio,
I have tried it with 777, even though 766 should be just as good.
Remember that it works when I execute it from the command line, and it works if I
create the file beforehand.
0
 

Author Comment

by:cbutton
ID: 1832270
julio,
With 777 I get the following message:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, Webmaster and inform them of the time the error occurred, and anything you might have done that may have caused
the error.

With 766 and oso's suggestion I get the message:

Can't open /web/guide/imiinc/orders/f.htm because Permission denied.


0
 

Author Comment

by:cbutton
ID: 1832271
Julio, new info.

With 777 on target directory, it actually does open a file called "f.htm," owned by "nobody," with permissions of 644. The file contains the word "ANYTHING," so the failure apparently is after the "print" statement.

Sorry for the spate of comments, but this is very important.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1832272
Does the sample program also get an Internal Server Error?

0
 
LVL 84

Expert Comment

by:ozo
ID: 1832273
Your sample program doesn't seem to print any html headers after writing to the file.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1832274
I'm not a Perl expert, but i'll dare showing the fix... :)

--//--
#!/usr/local/bin/perl5.003
require 'cgi-lib.pl';

print "Content-Type: text/html\n\n";
print "<HTML><BODY>\n";

$neworder = '/web/guide/imiinc/orders/f.htm';
open(ORDER,">$neworder") || &CgiDie("Can't open $neworder");
print ORDER "ANYTHING\n";
close(ORDER);

print "Process done.\n";
print "</BODY></HTML>\n";

exit(0);
--//--

To explain a bit more:

1. you need directory permissions set to 777 because your server - so your CGI process - runs as user 'nobody'; when you execute your CGI program from the command line instead, the CGI's UID is _your_ UID;

2. as ozo tells, whichever the task, you have to give back to the web server the needed header infos and - possibly - some content data.

HTH, julio
0
 

Author Comment

by:cbutton
ID: 1832275
It works!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question