3

I have a problem in writing data to a file using CGI. the code is as follows:

#!/usr/bin/perl
use strict;
use warnings;
use CGI ":standard";

print "Content-type:text/html \n\n";

open (FILE, '>', "file.txt") or die ("Could not open the file for writing...$!");
print FILE "something";
close(FILE);

The above code is giving the following error: "Could not open the file for writing...Permission denied at /var/www/cgi-bin/wfile.cgi line 8. The read and write permission for the file is already set... Please help!!!

2
  • for the www-user as well? The directory permissions too? Commented Dec 11, 2013 at 9:50
  • 1
    "The read and write permission for the file is already set" — to what? And what user does the web server run the script as? Commented Dec 11, 2013 at 9:50

1 Answer 1

2

The perl script will run as the user that apache/httpd/whatever is running as. You need to make sure that the directory and file is writeable by that user. Why not try writing to /tmp/ first.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, the best approach is a step-by-ste one, here... The problem is probably the lack for http server user permissions to write to that file... Unfortunately the methods to detect the http server user name depends on linux distribution, which is not specified in the question...
Yes.. its working now... but is it possible to redirect it to a file in another directory...?
Of course, you're specifying which file to open in your open() call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.