Solved

Open File for Write

Posted on 1998-04-11
11
181 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

939 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now