When writing to files, say a plain .txt file to log some info, I can only create/access the file if it is the same folder as the perl script. Is this a feature of perl cgis or should I update a setting for Apache, or am i being plain stupid?
Secondly, if I create a link to the text file from a file in htdocs when the text file is in /cgi-bin, the browser returns a server error as follows:
Server error!
Error message:
C:/apache/cgi-bin/course-files/counter.txt is not executable; ensure interpreted scripts have "#!" first line
It looks to me like the server thinks the .txt is a cgi file and is therefore trying to execute it. Is there somewhere in the apache httpd.conf where I can tell it how to treat .txt files?
Hope that rambling makes sense, all help much appreciated.
Cheers
You can open/create any file, as long as you enter the correct relative path to it. Also, ensure that the file (and directory) is write enabled. It is wise not to create text files in subdirectories of the cgi-bin directory.
If your cgi-bin is /home/blah/public_html/cgi-bin and you want to create a file in the /home/blah/public_html/stuff directory .. first, make sure that the stuff directory exists. Then, in your cgi script, the path would be ../stuff/name_of_file.txt
Question 2:
The server thinks that anything located in cgi-bin or one of the subdirectories is a cgi script, and is hence trying to execute the file instead of opening it. Move the file/folder to somewhere outside cgi-bin.
If I use:
open (FILE, feedback.txt");
This works fine. I have tried things including:
open (FILE, ">>../htdocs/feedback.txt");
open (FILE, ">>/htdocs/feedback.txt");
But these don't seem to work.
In my server config my documentroot is set to:
C:/apache/htdocs
My cgi-bin is at:
C:/apache/cgi-bin
Would it help if I put the cgi-bin in htdocs?
Any more help is gratefully received.
Cheers
Tried it, and it works!!!! Thank you very much - that is very useful.
Just so that I understand what is happening there then:
First off it looks like we are using the full drive path (c:\) rather than the webserver path, does that mean we cant do it with the webserver path?
Secondly the double backslash which I guess is just to escape the single backslash.
Cheers Damian, thats great.
I would expect
open(FILE, ">>\/htdocs\/feedback.txt");
to try to open
C:/apache/cgi-bin/htdocs/feedback.txt
Why not use your first solution with the ../../ ? I think (not sure) you could add a few extra of those if you're worried about using this syntax in other locations on your server..ie ../../../../../.. should get you to the root even if you are a few levels deeper...and it shouldn't matter if you're not.
For developing...If you add this line to the top of your script:
use CGI::Carp qw(carpout fatalsToBrowser);
and change the open command to this:
$file = "..\/..\/htdocs\/feedback.txt";
open(FILE, ">>$file") ¦¦ die "Problem opening $file: $!";
The script will stop and print any errors to your screen..mentioning what path and file it tried to open etc.
PS.change those ¦¦ pipes to 'real' pipes of course...not the ones with a split in the middle you see here (pipes are substituted by these chars in this forum)