Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cwd

Status
Not open for further replies.

kavi98

Programmer
Dec 14, 2000
48
0
0
US
I am running my script in the cgi-bin but i am storing files generated in the script three directory structure up to the location of the script i am giving absolute paths ,but when i run the script it generates an error.
I am using Cwd and then chdir to change to directory i want to store the file in .But it still does not work.


Thanks for all the help
kavi98
 
Remember that the directory structure that the web server sees is not the same one that your cgi program sees. For example, your cgi program may be, as far as the web server is concerned, in /cgi-local, but to your cgi program (and the cwd and chdir commands, etc.) it may be in /user/kavi98/ It's safest to use the $ENV{'DOCUMENT_ROOT'} environment variable as your "root" directory and base your directory structure off of that.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
can you give me an example of $ENV as i have never used it

Thanks
 
The %ENV hash contains all of your environment variables, including many that are set by the web server and/or browser. The variable $ENV{'DOCUMENT_ROOT'} should contain the "root" directory for your web site (something like "/user/kavi98/ Exactly what it's value is depends on the way your ISP set up the web server. Try printing it's value to see what it is.

To use it you do something like this:
Code:
chdir $ENV{'DOCUMENT_ROOT'} . "/myfiles/subdir";
In this case the directory myfiles would be at the same level as your cgi-bin directory, if you refer to cgi programs as
Does that help any?
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I still can't get the script to run ,i have assign the chdir to another variable is that the reason it breaks i tried debug and it shows no error on it.
$newFile = chdir $ENV{'DOCUMENT_ROOT'} . "/home/kavi98/textDir/";

Thanks
Kavi98
 
kavi,
'chdir' is a function that returns either a zero or a one. It looks like you are trying to use it to build the name of a new file, which will end up being 0 or 1. In the case above, I don't think you don't need to chdir at all.

$newFile = $ENV{'DOCUMENT_ROOT'}.'/home/kavi98/textDir/';
# $newFile might now contain '/mnt01/httdp/htdocs/home/kavi98/textDir/'
# assuming $ENV{'DOCUMENT_ROOT'} evaluates to '/mnt01/httpd/htdocs/'

open(NEWFILE,">$newFile") or die "Failed to open $newFile, $!\n";
print NEWFILE "Stuff to go in newFile";
close NEWFILE;

You do not need to chdir to create the new name, open the new file with the new name, print to it, and close it.

HTH


keep the rudder amid ship and beware the odd typo
 
In that example above $newFile contains only the name of the DIRECTORY, it doesn't have the file name on the end of it. Simply an oversight, I'm sure :) Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Kavi, did you print out $ENV{'DOCUMENT_ROOT'} to see what it contains?
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
i get 0 as the value of $ENV{'DOCUMENT_ROOT'}
but i still can't seem to transfer the file.

Thanks
For all the help
kavi98
 
You should get a directory path when you print $ENV{'DOCUMENT_ROOT'}. If it doesn't have a value, then you'll have to find out the full path name for where your web files are an use that. Either way, as long as the file path/name is valid the code goBoating showed above should work (with the addition of a file NAME on the end of the path name).
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top