Solved

Open File for Write

Posted on 1998-04-11
11
188 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OS Selection Menu 1 81
Power Action 2 38
How to create a registry Subkey containing the hostname 7 31
Notepad++ Question 11 21
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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