www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: Simple Create New File

  1. #1
    Join Date
    Nov 2002
    Posts
    82

    Simple Create New File

    Hello,

    This is my first attempt to create a new file in perl. I have this code sitting in my cgi-bin directory of my webserver:

    PHP Code:
    #!/usr/bin/perl

    use CGI ':standard';

    print 
    header();

    open (COLOURS,">colours.txt");
    print 
    COLOURS "My favourite colour is red\n";
    close COLOURS
    However, when I run this with my browser I get an empty screen (which is what I would expect) but no file seems to be created. I'm assuming the file would be created in my cgi-bin directory. My path to perl is 100% correct. Any ideas?

  2. #2
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    It runs as expected on Windows & Linux. Are you sure "colours.txt" was "NOT" created in your cgi-bin?

    What output do you get when you run the code below?
    Code:
    #!/usr/bin/perl
    
    use strict;
    use Cwd 'realpath';
    use CGI ':standard';
    use CGI::Carp 'fatalsToBrowser';
    
    my $file = 'cyco_colours.txt';
    
    open  COLOURS, ">$file" or die $!; 
    print COLOURS "My favourite colour is blue\n"; 
    close COLOURS;
    
    print header, "Full path to $file: ", realpath($file);

  3. #3
    Join Date
    Nov 2002
    Posts
    82
    I appreciate such a fast response.

    I'm positive it's not creating the file in the cgi-bin directory. I copy and pasted your code into a file called test.cgi and when I ran it I got this.

    Software error:

    Permission denied at /web/sites/www.domain.com/cgi-bin/test.cgi line 10.

    For help, please send mail to the webmaster (webmaster@domain.com), giving this error message and the time and date of the error.

  4. #4
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    OK, now we're getting somewhere. The error is very helpful in troubleshooting the problem, which is a permission issue. My guess is that your umask setting (configured on the server) is denying write access at the default level.

    Add this line right after opening the file.
    Code:
    chmod 0666, $file;
    If that doesn't do it, then we'll need to check your umask value and go from there.

  5. #5
    Join Date
    Nov 2002
    Posts
    82
    Thanks for the reply CyCo. I added your line and still have the same problem. Just to double check my code now looks like this:

    PHP Code:
    #!/usr/bin/perl

    use strict;
    use 
    Cwd 'realpath';
    use 
    CGI ':standard';
    use 
    CGI::Carp 'fatalsToBrowser';

    my $file 'cyco_colours.txt';

    open  COLOURS">$fileor die $!; 
    chmod 0666$file;
    print 
    COLOURS "My favourite colour is blue\n"
    close COLOURS;

    print 
    header"Full path to $file: "realpath($file); 
    What's the next debug step?

  6. #6
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    Bummer!

    What output do you get when you run this little snippet?
    Code:
    #!/usr/bin/perl
    
    use CGI ':standard';
    use CGI::Carp 'fatalsToBrowser';
    
    print header, umask;

  7. #7
    Join Date
    Nov 2002
    Posts
    82
    It outputted this:
    18
    What does that mean?

  8. #8
    Join Date
    Nov 2002
    Posts
    82
    BUMP...Anybody know what this means? Google isn't helping much.

  9. #9
    Join Date
    Nov 2006
    Posts
    140
    Idea:
    Change
    open (COLOURS,">colours.txt");
    to
    open (COLOURS,">./colours.txt");

  10. #10
    Join Date
    Nov 2002
    Posts
    82
    Nope that still doesn't work. Any ideas where the problem is? Might this be a server config error?

  11. #11
    Join Date
    Jan 2003
    Location
    USA
    Posts
    687
    After re-reading this thread, I believe the best way to get to the bottom of this is to contact your host and find out what restrictions they have configured for your site. If you are on a free host or some other type of restricted hosting plan, then you probably won't be allowed to do certain things.

  12. #12
    Join Date
    Nov 2006
    Location
    Oregon, USA
    Posts
    23
    There is one more thing to try. This is a nuts and bolts method, but it works for me.

    Remember you are trying to write to the directory in which the script is being executed,

    my $file = 'cyco_colours.txt';

    You may not have permissions to write to that directory. Another possibility is that depending on the server configuration, if the current path is not in @INC it may be trying to write to wherever perl thinks you are at that moment - which may be #!/usr/bin/perl. You certainly won't be allowed to write a file there.

    So begin by a) defining and using the full path to where you want to write, and b) making sure you have permissions to write to that location. In this scenario create a directory called data outside of your cgi-bin:

    /web/sites/www.domain.com/data/

    Chmod it to 666, if that doesn't work test chmod it to 777 just to see if it's a permissions issue.

    Second I like to write my own error handlers to avoid code and module bloat, but that's just me. Note my change in the or, and the added error subroutine. One last note - the $! variable holds system errors, so if the error is permission denied you can see it immediately in the response via the error sub.

    #!/usr/bin/perl

    $file = '/web/sites/www.domain.com/data/cyco_colours.txt';
    open COLOURS, ">$file" or &error("cannot write file $!");
    print COLOURS "My favourite colour is blue\n";
    close COLOURS;
    print "content-type: text/html\n\n";
    print "If you see this, $file was printed";

    sub error {
    my ($err);
    $err = shift(@_);
    print "content-type: text/html\n\n";
    print "Here is your error, fix it: $err";
    exit 0; ## to stop script immediately if you encounter an error
    }
    Last edited by rocknbil; 11-24-2006 at 04:27 AM. Reason: defined, denied, what's the difference . . .

  13. #13
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by telconstar99
    ...
    I have this code sitting in my cgi-bin directory of my webserver:

    PHP Code:
    #!/usr/bin/perl
    ...
    open (COLOURS,">colours.txt");
    ... 
    However, when I run this with my browser
    ...
    no file seems to be created. I'm assuming the file would be created in my cgi-bin directory.
    This is *hopefully* because the webserver process doesn't have write access to the directory the script is running in (as your say - presumably your cgi-bin directory)

    This is a ***GOOD*** thing.

    Your cgi-bin directory should *NOT* be writable by the user that the webserver runs the scripts as, otherwise, when someone finds something exploitable in one of your scripts (yes, I said "when", not "if" - because these things happen!) it will be simple for them to use that to create their own script in your cgi-bin directory and then run it to do whatever they want on your server that the user that your webserver runs as has access to do.

    Data files should either be created manually first
    (e.g. use 'touch colours.txt' on Linux/Unix, or 'type nul > colours.txt' on windows or create an empty colours.txt locally and upload it if you don't have shell access)
    and made so that the web process can write to them (if they need to be written to)
    (on Linux/Unix this is chown [WEBSERVER_USER] colours.txt and/or chmod +w colours.txt)
    (read http://www.linuxmanpages.com/man1/chown.1.php and http://www.linuxmanpages.com/man1/chmod.1.php )

    Or... better still, they should preferrably be stored in another directory that the webserver process can read from/write to but not execute programs in.

    (if someone could write to colours.txt they may be able to write code into that and run it too if it is executable (??[x]??[x]??[x] on Linux/Unix) - unless your webserver only runs scripts by extension (e.g. will run .cgi or .pl but not .txt) ( as per AddHandler cgi-script (whatever) and ExecCGI )

    John
    Last edited by JohnGH; 11-27-2006 at 11:45 AM.

  14. #14
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by rocknbil
    So begin by a) defining and using the full path to where you want to write, and b) making sure you have permissions to write to that location. In this scenario create a directory called data outside of your cgi-bin:

    /web/sites/www.domain.com/data/

    Chmod it to 666, if that doesn't work test chmod it to 777 just to see if it's a permissions issue.
    Don't chmod dirs to 666 - dirs need to be executable to be used... try this:

    Code:
    $ mkdir test
    $ chmod 666 test
    $ touch test/file
    touch: cannot touch `test/file': Permission denied
    $ cd test
    -bash: cd: test: Permission denied
    $ chmod +x test
    $ touch test/file
    $ cd test/
    $ pwd
    /home/john/test
    $ ls -la .
    total 8
    drwxrwxrwx   2 john john 4096 2006-11-27 14:21 .
    drwxr-xr-x  10 john john 4096 2006-11-27 14:19 ..
    -rw-r--r--   1 john john    0 2006-11-27 14:21 file
    IHTH

    Kind regards,

    John

  15. #15
    Join Date
    Nov 2006
    Posts
    140
    I don't think that is wise.
    You got +rwx or 777 for your directories
    That means you are letting anyone do anything
    Directories should be 711 or 755 (eXecute or Read + eXecute)
    Don't give write permission on a directory.
    Only on select files.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles