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

Hi Monks,

I hope one of you lot can help me with this as i am not all that clued up with directory permissions.

I have a form that takes input from users on the web and passes it to a script for processing. I would then like the script to write a file, with the name based on date and time (so that only one entry exists per file) to a directory on my server.

I have heard that writing files to the webroot (i.e in the cgi-bin dir) is not advised, so i opted to write the file to a directory within /var/log/.

Below is the bit of code i have wrote:
open (LOGFILE, ">> /var/log/webd/input.log") ; print LOGFILE "$userwebinput\n" ; close (LOGFILE) ;

This will obviosly only create a file called input.log and not one based on the time and date.
Also, What permissions will the /var/log/webd/ directory need to allow the script to write files that currently do not exist to it?

Replies are listed 'Best First'.
Re:(fireartist) Writing Files To Directorys
by fireartist (Chaplain) on Aug 30, 2002 at 09:05 UTC
    Without knowing what user you are logging in as (and therefore what user your files are running as), and more about your directory setup, I'll just describe how I do this.

    My home directory is /home/username/www.domain.com
    I ftp / telnet into the box using the same username, so I know the files will be owned by the same user that owns the directories.
    My home directory has 2 sub-dirs named public_html and cgi-bin. So I create another sub-dir named data and change the permissions to 777 or drwxrwxrwx (check this with ls -l) - or 755 if the files are owned by the same user the web-server is running as.
    The full path to this directory is /home/username/www.domian.com/data

    If your script is getting user input, then it should be running under taint mode.
    #!/usr/bin/perl -wT
    And the warnings switch will also help figure out what's going wrong.

    Regarding creating a filename using the date/time, a search of perlmonks for "filename date time" gives the following results (among others).
    Filenames based on timestamp
    create file with current date for name
    Renaming file with date
    Date in filename

    update: corrected required file permissions from 666 to 777 and clarified, thanks to grinder!
      Its going to be running as webd (my webserver). I tried your searches, but there were no results returned
Re: Writing Files To Directorys
by BrowserUk (Patriarch) on Aug 30, 2002 at 08:30 UTC
      thanks. how about creating the file names based on date/time bit?

        If just a numeric value representing the time is okay for your purposes, see perlfunc:time - it gives you a large integer value representing the current time to the nearest second.

        If you want the filenames to be easily readable as date and time by humans looking in directories then look at perfunc:localtime.

        BTW. If your had typed "time" into the white box at the top of your screen and clicked the search button, it would have taken you directly to the first reference above.

        As for creating the file, type "open" in that same box and you will get a whole host of information, examples and further links for you to explore.

        If you get problems, post your code and explain what your problem is, and someone will help your further.


        Well It's better than the Abottoire, but Yorkshire!