Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl CGI - reading and Writing

         

Robber

8:48 am on Jun 18, 2002 (gmt 0)

10+ Year Member



Im working my way through a Perl course over here in the UK and I have a question thats not answered in the book.

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

DrDoc

2:53 am on Jun 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Question 1:

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.

Robber

8:12 am on Jun 19, 2002 (gmt 0)

10+ Year Member



Cheers Doc, thanks for your help. Your answer to Q2 makes sense, I suspected something along those lines. Only trouble now is still can't get my scrips to write to a file in another dir.

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

Damian

10:02 am on Jun 19, 2002 (gmt 0)

10+ Year Member



Hi Robber,

You working on a win32 Apache setup ?

try this:

open(FILE, ">>C:\\apache\\htdocs\\feedback.txt");

Robber

10:23 am on Jun 19, 2002 (gmt 0)

10+ Year Member



Damian,

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.

Robber

10:32 am on Jun 19, 2002 (gmt 0)

10+ Year Member



Just tried this:
open(FILE, ">>..\/..\/htdocs\/feedback.txt");

And that works too, excellent. Is it possible to not need the ../../ and just start with a /? I tried:

open(FILE, ">>\/htdocs\/feedback.txt");

But this didnt do the job. Any ideas?

Cheers

Damian

11:09 am on Jun 19, 2002 (gmt 0)

10+ Year Member



The second backslash in the double backslashes is just an escape of the first yes.

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)

Robber

11:12 am on Jun 19, 2002 (gmt 0)

10+ Year Member



Cheers Damian,

I think I'll do that

Edge

11:39 am on Jun 19, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have to know your actual root directory to etablish your directory path. Substitute the following in your script for the root path. Add on as neccesary to complete the path to the directory/file you wish to write to.

$rootdir = "$ENV{DOCUMENT_ROOT}/";