سؤال

current folder structure is as follows

/main/site/script.cgi

I want to be able to write to a file at

main/logs/mylog.log

How can I do this without giving the absolute path ? since This can be deployed to different servers, I wont want to have to change this every time its deployed.

هل كانت مفيدة؟

المحلول

To access a directory using a relative path, you need to establish first what that path will be relative to. That means you need to find out what the current working directory is for running CGI scripts.

If you add this program to your server and call it up on a browser you will see the current working directory that that server applies to all of its CGI programs.

use strict;
use warnings;

use CGI ':standard';
use Cwd 'getcwd';

print header('text/plain');

printf "Current working directory: %s\n", getcwd;

If, for instance, you find that the current working directory is /main/site then you can create the file using the path ../logs/mylog.log.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top