Solved

Open File for Write

Posted on 1998-04-11
11
180 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
Comment Utility
What does it say if you do
&CgiDie("Can't open $neworder because $!");
0
 
LVL 84

Expert Comment

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

Accepted Solution

by:
julio011597 earned 170 total points
Comment Utility
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
Comment Utility
Sorry ozo, i had this open before you put your first comment :(
0
 

Author Comment

by:cbutton
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cbutton
Comment Utility
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
Comment Utility
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
Comment Utility
Does the sample program also get an Internal Server Error?

0
 
LVL 84

Expert Comment

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

Expert Comment

by:julio011597
Comment Utility
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
Comment Utility
It works!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now