Solved

Open File for Write

Posted on 1998-04-11
11
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

696 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