Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Sorry in advance if this question is so elementary. I did look through dozens of old questions and couldn't find the answer.

This script works fine when I run it from the command line:

#!/usr/bin/perl -w $user_name = "test_dir"; mkdir ("$user_name",0777) || die "cannot mkdir : $!"; print "Content-type: text/html\n\n"; print "did it make the directory? \n";

But when you hit it through a web brwoser it gives you a 500 internal server error. I'm sure I'm missing something simple... but I sure can't figure it out.

Ashley

ashleyscottmeyers@hotmail.com

Replies are listed 'Best First'.
Re: mkdir through a web interface
by Masem (Monsignor) on Oct 04, 2001 at 23:08 UTC
    Since you did catch any possible mkdir errors, these will, as you have it, be caught by the web server's error log. You can use the CGI::Carp with fatalsToBrowser to have the errors reported to the browser.

    As for the error, the script will be running as the user that the web server is running under; if the directory that is indicated is in a directory that is not writable by that particularly user, mkdir will fail with write permissions. When you ran it from the command line, it uses your permissions, which are probably sufficient to complete the operation. In addition, you will be making the directory in the same location where the CGI script is being run, whcih is probably not your goal; you probably want to use absolute paths to point to exactly where you want the directory to be made.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: mkdir through a web interface
by higle (Chaplain) on Oct 04, 2001 at 23:06 UTC
    Make sure that you've changed the permissions on your script file so that it's executable, i.e. (if you're running under Unix):
    chmod 0755 foo.pl

    It's also a good idea to check that the directory that you're trying to create a directory in has write permissions for the webserver... you definitely want to contain such scripts as this in their own subdirectories of the cgi-bin directory, and grant permissions to just those directories, or you're looking at possible security holes.

    higle
Re: mkdir through a web interface
by thunders (Priest) on Oct 05, 2001 at 00:44 UTC
    you may want to consider using CGI.pm for it's header, start_html and end_html functions. It may not be directly relevant to your problem, but it will help you avoid those 500 server errors if you feed the server a properly formed web page. You are asking it to display "text/html" perhaps you really want "text/plain."
      You can mod me down for this as you like, but I see an incessantly overuse of modules going on around here. Everybody cries if you don't use CGI... even for something this simple. Ashley is merely printing ONE line to the screen, I hardly feel this requires the use of CGI.pm.. Now I'm not discrediting the use of modules or CGI.pm in general, I just think that sometimes you guys go a little overboard with it. Hell no I'm not going to use CGI to print one line. That's stupid. Not only that but your reply is out of context with the original question. What exactly does CGI do that's going to stop the 500 errors?

      And to address Asley's qustion, the simple answer is probably file permissions.. Namely (as stated above) the permissions associated with the parent directory, the one you are attempting to create directories in, needs to be writable by the server.
        Thanks all. I swore I checked all the premissions, but I guess not. I've got it working now, though. Thanks again. Ashley

        I wouldn't mod you down, but I'd still use CGI.pm, even for something this simple, mainly because it takes care of the fact that improperly capitalized headers will make your page behave strangely on some browsers. I'd rather not worry about it, and it's quicker to develop a script like this with CGI.pm than to go and check the exact, proper method to make a header.

        Do I use strict and taint checking for little scripts like this that don't take input? Nope. But for everything longer than 5 lines or that takes input I do, just in case.

        -Any sufficiently advanced technology is
        indistinguishable from doubletalk.

Re: mkdir through a web interface
by dirthurts (Hermit) on Oct 04, 2001 at 23:41 UTC
    The short answer is that you called die before you sent the content-type header.

    So your script is failing there, move the content header up and you will see the error message.

    Jay

      That is not what is causing the 500 internal server error, dirthurts. I ran the script on my webserver, setting the permissions correctly for the directories and script file, and it created the "test_dir" directory just fine.

      Just Another Permissions Hangup :)

      higle
        well, yes... if it creates the directory just fine then it doesn't have to die, does it? It is imperative to send a header before anything that might send any output.

        Not trying to start a fight, just trying to point Anonymous Monk in the right direction.

        Jay

Re: mkdir through a web interface
by descartes (Friar) on Oct 05, 2001 at 16:57 UTC

    I think your problem is elementary. Try rewriting this line:

    mkdir ("$user_name",0777) || die "cannot mkdir : $!";

    As this:

    mkdir ($user_name, 0777) || warn "directory $! already exists";

    I hope this helps.

    Brother Descartes
    -- Programmus, ergo sum.

      I was having a problem very much like that. Permissions, were not allowing me to print to a log file.
      I don't like CGI.pm my self. :)
      I like doing it by hand, It looks nicer. I however do use CGI::Cookie (only for generating the cookies, not for sending.) After all, if I don't keep me code clean, people get angry, and the PHP barbarians point at me and mutter. :)

      NS6.1 really socks with forms. :-(