Re: Perl CGI does not generate file.
From: Gazza (gazza_at_192.168.1.1)
Date: 01/14/05
- Next message: Jim Monty: "Re: Unix-format text from STDOUT under Windows (AcitvePerl 5.8.x)?"
- Previous message: A. Sinan Unur: "Re: Adding a delimiter inbetween number characters and letter characters"
- In reply to: Paul Lalli: "Re: Perl CGI does not generate file."
- Next in thread: Scott Bryce: "Re: Perl CGI does not generate file."
- Reply: Scott Bryce: "Re: Perl CGI does not generate file."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 14 Jan 2005 01:37:57 GMT
Paul Lalli wrote:
> "Gazza" <gazza@192.168.1.1> wrote in message
> news:rJuFd.169$VE5.153@newsfe2-win.ntli.net...
>> I have written an HTML script
>
> This is a contradiction in terms
Oops, you're right! Sorry. :-)
>> The HTML script is here:
>>
>
http://homepage.ntlworld.com/gary.hayward/My_HTML_Scripts/perl_cgi_file_creator.html
>>
>> (cut and paste from source view) and the Perl CGI script is here:
>>
>>
>
http://homepage.ntlworld.com/gary.hayward/My_Perl_Scripts/perl_cgi_file_creator.txt
>>
>
> Why didn't you just copy and paste the code yourself? That would have
> been far more polite. Regardless:
All the scripts (those that really are: BASH, Perl, etc) on my web site for
downloading are in plain text so I thought I would have my HTML pages like
that, but then I thought, by leaving them with the '.html' extension,
people could see what they looked like straight away -- only a minor thing,
I know, but they are HTML on a web server. I used to include code in my
newsgroup postings but thought, with two longish files it might look a mess
and be irritating. I do go and am going to go to considerable lengths to
make stuff I make available for others easy to access and use on an ongoing
basis.
>
>> $input3=<STDIN>;
>> $input3=~s/\+/ /g;
>> @input4 = split(/&/, $input3);
>> foreach $time(0,1) {
>> $input4[$time]=~s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
>> $input4[$time]=~s/</</g;
>> }
>> $input4[0]=~s/nameoffiletocreate=//;
>> $input4[1]=~s/filetext=//;
>>
>> print <<endofhtml;
>
> <HTML snipped>
>
> You are attempting to parse CGI parameters yourself. This is a bad
> idea. You should be using a module that does this task for you. That
> module is CGI.pm. Please read up on it at
> perldoc CGI
Thanks and I'll bear that in mind :-) (although, as a 'newbie', I feel I am
going to have to be doing without calling modules for the time being so I
can learn the basics of the language by doing things the
'hard'/'long-winded' way).
>
> Using that module, the entirety of the above code would be reduced to:
> @input4 = (param('nameoffiletocreate'), param('filetext'));
>
>> endofhtml
>>
>> open WF,">$input4[0]";
>> print WF "$input4[1]";
>> close WF;
>
>> What have I done wrong?
>
> You have not checked the return value of open. You have no idea if this
> open succeeded. Perl will tell you that, and will also tell you *why*
> it failed - but you have to tell it to tell you:
>
> open WF "> $input4[0]" or die "Could not open $input4[0] for writing:
> $!";
>
The 'die' stuff was on my computer's version of the Perl script and I didn't
update the web-hosted one which I should have done.
> Now if that open fails, your script will terminate, and the error
> message will be printed to your error log. If you do not have access to
> the error log, add this line to the top of your script:
>
> use CGI::Carp qw/fatalsToBrowser/;
>
> That will cause the error to be displayed in your browser.
Thank you. That code was very helpful.
>
> Once you have done this, you will likely be told exactly why your
> program fails. As an offhand guess, I'd bet the CGI user doesn't have
> permissions to create a new file.
>
The error I got was that that the file couldn't be written as there was no
such file existing. I thought that may be due to the script not having the
right permission on directories it needed to go through to get to where it
had to write the file (the Opera browser I use simply reports, 'Could not
open file' when browsing to a directory I don't have permission to access;
it doesn't tell you everything). Trying a different directory, I got
permission denied messages despite all the directories in the file's path
being owned by and set up with the right permissions for the user running
the browser and invoking the Perl script. The only time I got it to work
was when I specified '/tmp/[file_name]' as the destination. The HTML page
(watered down) and Perl CGI script are part of a web-based web site
administrator's tool set that I've written for myself (in case anybody was
wondering, which you probably weren't :-)) to administrate one of my web
sites. One Perl script that I have uploaded to my web server does manage
to create files without any problems (it's used to log visitor's IP
addresses and other visitor stats in a file) so, hopefully, this admin.
Perl script will work there (although I need to look at how secure things
would be before I implement it there).
> Paul Lalli
Thanks for your help.
Yours,
Gary Hayward.
- Next message: Jim Monty: "Re: Unix-format text from STDOUT under Windows (AcitvePerl 5.8.x)?"
- Previous message: A. Sinan Unur: "Re: Adding a delimiter inbetween number characters and letter characters"
- In reply to: Paul Lalli: "Re: Perl CGI does not generate file."
- Next in thread: Scott Bryce: "Re: Perl CGI does not generate file."
- Reply: Scott Bryce: "Re: Perl CGI does not generate file."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|