[31386] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2638 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 19 14:15:09 2009

Date: Mon, 19 Oct 2009 11:14:58 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 19 Oct 2009     Volume: 11 Number: 2638

Today's topics:
        Can't open an output file from a perl CGI script. <daves@orpheusmail.co.uk>
    Re: Can't open an output file from a perl CGI script. <peter.tuente@materna.de>
    Re: Can't open an output file from a perl CGI script. <tadmc@seesig.invalid>
    Re: Can't open an output file from a perl CGI script. <spamtrap@shermpendley.com>
    Re: Can't open an output file from a perl CGI script. <daves@orpheusmail.co.uk>
    Re: Can't open an output file from a perl CGI script. <daves@orpheusmail.co.uk>
    Re: Can't open an output file from a perl CGI script. <daves@orpheusmail.co.uk>
    Re: Can't open an output file from a perl CGI script. <pgovern@u.washington.edu>
    Re: Can't open an output file from a perl CGI script. <smallpond@juno.com>
    Re: Can't open an output file from a perl CGI script. <tadmc@seesig.invalid>
    Re: Can't open an output file from a perl CGI script. <tadmc@seesig.invalid>
    Re: Can't open an output file from a perl CGI script. <smallpond@juno.com>
    Re: Can't open an output file from a perl CGI script. <hjp-usenet2@hjp.at>
    Re: Can't open an output file from a perl CGI script. <mvdwege@mail.com>
    Re: Can't open an output file from a perl CGI script. <daves@orpheusmail.co.uk>
    Re: Can't open an output file from a perl CGI script. <jurgenex@hotmail.com>
    Re: Can't open an output file from a perl CGI script. <sreservoir@gmail.com>
    Re: Can't open an output file from a perl CGI script. <daves@orpheusmail.co.uk>
    Re: Can't open an output file from a perl CGI script. <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 16 Oct 2009 13:47:48 +0100
From: Dave Stratford <daves@orpheusmail.co.uk>
Subject: Can't open an output file from a perl CGI script.
Message-Id: <50ab236393daves@orpheusmail.co.uk>

Hi folks,

I'm not sure whether this is a Perl or a Linux issue.

I'm developing CGI stuff, in perl, that will allow a user to upload a file
from a web browser, which the perl script then places into specific
directory.

Most of it works, except that perl won't open the file. There's no useful
error given, and I don't know which logs to look in, I've tried the httpd
logs, but they are empty for some reason.

Is it possible that it is simply a linux permissions fault somewhere, and
if so, how do I identify it?

If I ask Perl to open a file, and then include a directory that doesn't
currently exist within the file mame, will the directory be created?

The relevant bit of the perl script looks like this:

  my $phs   = param("phs");
  my $img;
  my $modno = param("modno");
  my $valid = 1;
  my $ext;
  my $fh;

  $phs = "" unless defined($phs);

  if ( $phs eq "" )
  {
    $template->param(NOPHOTO=>1);
  }
  else
  {
    $mt1 = uploadInfo($phs)->{'Content-Type'} if $phs ne "";
    if ( $mt1 ne "" && $mt1 ne "image/jpeg" && $mt1 ne "image/png" )
    { 
      $valid = 0;
      $template->param(P1ERR=>1);
    }


    if ($valid)
    {
      if ($phs ne "")
      {
        read ($phs, $img, (stat ($phs))[7] == (stat ($phs))[7]);
        $ext = $mt1 eq "image/jpeg" ? ".jpeg" : ".png";
        open ($fh,">","~/$modno-headshot$ext")
                             or die "can't create image file: $1";
        binmode $fh;
        while (<$phs>)
        {
          print $fh $_;
        }
        close $fh;
      }
    }
  }

The error I'm getting is : 'can't create image file:  at
/var/www/cgi-bin/application.pl line 1816

Unfortunately this tells me only that it is the open statement that is
failing.

I've tried changing the '~/' to '/home/dave/' to /var/www/html/ to
/var/www/cgi-bin/' and even removing that bit altogether. No matter what I
do, it fails on the open.

Any ideas gratefully received.

Thanks,

Dave

-- 
Dave Stratford    ZFCA
http://daves.orpheusweb.co.uk/
Hexagon Systems Limited - Experts in VME systems development



------------------------------

Date: Fri, 16 Oct 2009 15:01:57 +0200
From: Peter Tuente <peter.tuente@materna.de>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <hb9qs4$67a$1@pentheus.materna.de>

Dave Stratford schrieb:
> Hi folks,
> 
> I'm not sure whether this is a Perl or a Linux issue.
> 
> I'm developing CGI stuff, in perl, that will allow a user to upload a file
> from a web browser, which the perl script then places into specific
> directory.
> 
> Most of it works, except that perl won't open the file. There's no useful
> error given, and I don't know which logs to look in, I've tried the httpd
> logs, but they are empty for some reason.
> 
> Is it possible that it is simply a linux permissions fault somewhere, and
> if so, how do I identify it?
> 
> If I ask Perl to open a file, and then include a directory that doesn't
> currently exist within the file mame, will the directory be created?
> 
> The relevant bit of the perl script looks like this:
> 
>   my $phs   = param("phs");
>   my $img;
>   my $modno = param("modno");
>   my $valid = 1;
>   my $ext;
>   my $fh;
> 
>   $phs = "" unless defined($phs);
> 
>   if ( $phs eq "" )
>   {
>     $template->param(NOPHOTO=>1);
>   }
>   else
>   {
>     $mt1 = uploadInfo($phs)->{'Content-Type'} if $phs ne "";
>     if ( $mt1 ne "" && $mt1 ne "image/jpeg" && $mt1 ne "image/png" )
>     { 
>       $valid = 0;
>       $template->param(P1ERR=>1);
>     }
> 
> 
>     if ($valid)
>     {
>       if ($phs ne "")
>       {
>         read ($phs, $img, (stat ($phs))[7] == (stat ($phs))[7]);
>         $ext = $mt1 eq "image/jpeg" ? ".jpeg" : ".png";
>         open ($fh,">","~/$modno-headshot$ext")
>                              or die "can't create image file: $1";
>         binmode $fh;
>         while (<$phs>)
>         {
>           print $fh $_;
>         }
>         close $fh;
>       }
>     }
>   }
> 
> The error I'm getting is : 'can't create image file:  at
> /var/www/cgi-bin/application.pl line 1816
> 
> Unfortunately this tells me only that it is the open statement that is
> failing.
> 
> I've tried changing the '~/' to '/home/dave/' to /var/www/html/ to
> /var/www/cgi-bin/' and even removing that bit altogether. No matter what I
> do, it fails on the open.
> 
> Any ideas gratefully received.
> 
> Thanks,
> 
> Dave
> 
Hi Dave,

I think that there is a linux permission problem. The Web-User is 
probably not allowed to create a file in the desired directory.

Try to change the access rights of the directory to writable for 
everyone by issuing the following command as user 'dave':

	chmod 777 /home/dave

then change the open line to

	open ($fh,">","/home/dave/$modno-headshot$ext")

 .. and try again.

Regards

PiT


------------------------------

Date: Fri, 16 Oct 2009 08:53:10 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <slrnhdgtpl.h2h.tadmc@tadmc30.sbcglobal.net>

Dave Stratford <daves@orpheusmail.co.uk> wrote:

> I'm developing CGI stuff, in perl, that will allow a user to upload a file
> from a web browser, which the perl script then places into specific
> directory.
>
> Most of it works, except that perl won't open the file. There's no useful
> error given, 


That is because you have not asked for a useful message.

(an "error" is seldom useful, an "error message" is often useful...)


> and I don't know which logs to look in, 


The webserver error log.


> I've tried the httpd
> logs, but they are empty for some reason.


Errr, then where does the message you included below show up?


> Is it possible that it is simply a linux permissions fault somewhere, and
> if so, 


Yes.


> how do I identify it?


By asking for useful messages.


> If I ask Perl to open a file, and then include a directory that doesn't
> currently exist within the file mame, will the directory be created?


No.


> The relevant bit of the perl script looks like this:
>
>   my $phs   = param("phs");
>   my $img;
>   my $modno = param("modno");
>   my $valid = 1;
>   my $ext;
>   my $fh;
>
>   $phs = "" unless defined($phs);


You should declare variables when you first use them, rather than
all at the top.

    
>   if ( $phs eq "" )
    ^^^^^^^^^^^^^^^^^
>   {
>     $template->param(NOPHOTO=>1);
>   }
>   else
>   {
>     $mt1 = uploadInfo($phs)->{'Content-Type'} if $phs ne "";
                                                ^^^^^^^^^^^^^

That doesn't do anything. 

Control can reach that statement only if $phs already ne ""...


>     if ( $mt1 ne "" && $mt1 ne "image/jpeg" && $mt1 ne "image/png" )
>     { 
>       $valid = 0;
>       $template->param(P1ERR=>1);
>     }
>
>
>     if ($valid)
>     {
>       if ($phs ne "")
>       {
>         read ($phs, $img, (stat ($phs))[7] == (stat ($phs))[7]);
                ^^^^                         ^^
                ^^^^                         ^^


WTF?

The first argument to read must be a filehandle. 

$phs is a string, not a filehandle.

    You should always enable warnings when developing Perl code!
    (ask for useful messages, part 1)

There is no point in using a boolean operation if you are not
going to test the value of it somehow...

Also you can replace

    (stat ($phs))[7]

with

    -s $phs

to get the file size.


>         $ext = $mt1 eq "image/jpeg" ? ".jpeg" : ".png";
>         open ($fh,">","~/$modno-headshot$ext")
>                              or die "can't create image file: $1";
                                                                ^^
                                                                ^^

You are using the wrong variable. The $! variable contains the
reason for open's failure.
(ask for useful messages, part 2)

Include the delimited name of the file you are trying to open
in your diagnostic message.

    open ($fh, '>',"$modno-headshot$ext")
        or die "can't create image file '$modno-headshot$ext': $!";

> The error I'm getting is : 'can't create image file:  at
> /var/www/cgi-bin/application.pl line 1816


Where are you "getting" that message from?

That is an awful lot of lines for a single file, consider
modularizing your code some...


> Unfortunately this tells me only that it is the open statement that is
> failing.


Because that is all you asked for.


> I've tried changing the '~/' 


Good, because that is a feature of the shell, and Perl is not the shell.

(and because the user the CGI program runs as likely does not even
 have a home directory...)


> to '/home/dave/' to /var/www/html/ to
> /var/www/cgi-bin/' and even removing that bit altogether. No matter what I
> do, it fails on the open.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Fri, 16 Oct 2009 10:47:39 -0400
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <m2oco7pf90.fsf@shermpendley.com>

Dave Stratford <daves@orpheusmail.co.uk> writes:

> Is it possible that it is simply a linux permissions fault somewhere, and
> if so, how do I identify it?

It's almost certainly a permissions issue.

> If I ask Perl to open a file, and then include a directory that doesn't
> currently exist within the file mame, will the directory be created?

No, the directory must already exist.

>         open ($fh,">","~/$modno-headshot$ext")
>                              or die "can't create image file: $1";
 ...
> The error I'm getting is : 'can't create image file:  at
> /var/www/cgi-bin/application.pl line 1816
>
> Unfortunately this tells me only that it is the open statement that is
> failing.

Perl is a hostile witness - it knows what went wrong but won't volunteer
that information, so you have to ask for it directly. The reason *why* the
open failed is in $!, so I'd change the above to:

    open ($fh, ">", "~/$modno-headshot$ext")
        or die "can't create image file '$1': $!";

First, including single quotes around $1 in the error message makes it
obvious if there are any issues with leading or trailing whitespace such
as newlines. And second, including $! in the error message will tell
you why the open failed.

sherm--


------------------------------

Date: Fri, 16 Oct 2009 17:33:07 +0100
From: Dave Stratford <daves@orpheusmail.co.uk>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <50ab380389daves@orpheusmail.co.uk>

In article <slrnhdgtpl.h2h.tadmc@tadmc30.sbcglobal.net>,
   Tad J McClellan <tadmc@seesig.invalid> wrote:
> Dave Stratford <daves@orpheusmail.co.uk> wrote:

> > I'm developing CGI stuff, in perl, that will allow a user to upload a
> > file from a web browser, which the perl script then places into
> > specific directory.
> >
> > Most of it works, except that perl won't open the file. There's no
> > useful error given, 


> That is because you have not asked for a useful message.

> (an "error" is seldom useful, an "error message" is often useful...)


> > and I don't know which logs to look in, 


> The webserver error log.


> > I've tried the httpd
> > logs, but they are empty for some reason.


> Errr, then where does the message you included below show up?

In the web browser window, obviously.


> > Is it possible that it is simply a linux permissions fault somewhere,
> > and if so, 


> Yes.


> > how do I identify it?


> By asking for useful messages.


> > If I ask Perl to open a file, and then include a directory that doesn't
> > currently exist within the file mame, will the directory be created?


> No.


> > The relevant bit of the perl script looks like this:
> >
> >   my $phs   = param("phs");
> >   my $img;
> >   my $modno = param("modno");
> >   my $valid = 1;
> >   my $ext;
> >   my $fh;
> >
> >   $phs = "" unless defined($phs);


> You should declare variables when you first use them, rather than
> all at the top.

I did say relevant bit of code. This is part of a subroutine.

>     
> >   if ( $phs eq "" )
>     ^^^^^^^^^^^^^^^^^
> >   {
> >     $template->param(NOPHOTO=>1);
> >   }
> >   else
> >   {
> >     $mt1 = uploadInfo($phs)->{'Content-Type'} if $phs ne "";
>                                                 ^^^^^^^^^^^^^

> That doesn't do anything. 

> Control can reach that statement only if $phs already ne ""...

Noted. Comes from repeated editing and adding and removing bits.

> >     if ( $mt1 ne "" && $mt1 ne "image/jpeg" && $mt1 ne "image/png" )
> >     { 
> >       $valid = 0;
> >       $template->param(P1ERR=>1);
> >     }
> >
> >
> >     if ($valid)
> >     {
> >       if ($phs ne "")
> >       {
> >         read ($phs, $img, (stat ($phs))[7] == (stat ($phs))[7]);
>                 ^^^^                         ^^
>                 ^^^^                         ^^


> WTF?

> The first argument to read must be a filehandle. 

$phs is a file handle. The second bit confuses me as well. I copied it
from a bit of code that already works - that I didn't write.


> $phs is a string, not a filehandle.

Actually it's both.

>     You should always enable warnings when developing Perl code!
>     (ask for useful messages, part 1)

Already have done, at the top of the code I already have:
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

> There is no point in using a boolean operation if you are not
> going to test the value of it somehow...

> Also you can replace

>     (stat ($phs))[7]

> with

>     -s $phs

> to get the file size.

Okay.

> >         $ext = $mt1 eq "image/jpeg" ? ".jpeg" : ".png";
> >         open ($fh,">","~/$modno-headshot$ext")
> >                              or die "can't create image file: $1";
>                                                                 ^^
>                                                                 ^^

> You are using the wrong variable. The $! variable contains the
> reason for open's failure.
> (ask for useful messages, part 2)

Oops. Typo! Thanks.

> Include the delimited name of the file you are trying to open
> in your diagnostic message.

>     open ($fh, '>',"$modno-headshot$ext")
>         or die "can't create image file '$modno-headshot$ext': $!";

> > The error I'm getting is : 'can't create image file:  at
> > /var/www/cgi-bin/application.pl line 1816


> Where are you "getting" that message from?

The browser window, obviously.

> That is an awful lot of lines for a single file, consider
> modularizing your code some...

This is the subroutine to do the file handling.

I've now changed the open to read :

        $img = "/home/dave/UploadedPhotos/$mod_no-headshot$ext"
        open ($fh,">",$img)
             or die "can't create file  '$img' : '$!'";

I've created the UploadedPhotos directory, and done a chmod 777 on it.

Still failing on the open, now with a permissions error:

can't create file '/home/dave/UploadedPhotos/3082-headshot.jpeg' :
'Permission denied' at /var/www/cgi-bin/application.pl line 1820.

Dave

-- 
Dave Stratford    ZFCA
http://daves.orpheusweb.co.uk/
Hexagon Systems Limited - Experts in VME systems development



------------------------------

Date: Fri, 16 Oct 2009 17:36:22 +0100
From: Dave Stratford <daves@orpheusmail.co.uk>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <50ab385029daves@orpheusmail.co.uk>

In article <hb9qs4$67a$1@pentheus.materna.de>,
   Peter Tuente <peter.tuente@materna.de> wrote:
> Dave Stratford schrieb:
> > Hi folks,
> > 
> > I'm not sure whether this is a Perl or a Linux issue.
> > 
> > I'm developing CGI stuff, in perl, that will allow a user to upload a
> > file from a web browser, which the perl script then places into
> > specific directory.
> > 
> > Most of it works, except that perl won't open the file. There's no
> > useful error given, and I don't know which logs to look in, I've tried
> > the httpd logs, but they are empty for some reason.
> > 
> > Is it possible that it is simply a linux permissions fault somewhere,
> > and if so, how do I identify it?
> > 
> > If I ask Perl to open a file, and then include a directory that doesn't
> > currently exist within the file mame, will the directory be created?
> > 

> > 
> > Any ideas gratefully received.
> > 
> > Thanks,
> > 
> > Dave
> > 
> Hi Dave,

> I think that there is a linux permission problem. The Web-User is 
> probably not allowed to create a file in the desired directory.

> Try to change the access rights of the directory to writable for 
> everyone by issuing the following command as user 'dave':

> 	chmod 777 /home/dave

> then change the open line to

> 	open ($fh,">","/home/dave/$modno-headshot$ext")

I've set up a separate directory: /home/dave/UplodedPhotos changed the 
permissions as you suggested, (and the code to go with it!), but I'm still
getting an error. I have worked out that it's a permissions error though.

> .. and try again.

Thanks,

Dave

-- 
Dave Stratford    ZFCA
http://daves.orpheusweb.co.uk/
Hexagon Systems Limited - Experts in VME systems development



------------------------------

Date: Fri, 16 Oct 2009 17:39:01 +0100
From: Dave Stratford <daves@orpheusmail.co.uk>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <50ab388ea7daves@orpheusmail.co.uk>

In article <m2oco7pf90.fsf@shermpendley.com>,
   Sherm Pendley <spamtrap@shermpendley.com> wrote:
> Dave Stratford <daves@orpheusmail.co.uk> writes:

> > Is it possible that it is simply a linux permissions fault somewhere,
> > and if so, how do I identify it?

> It's almost certainly a permissions issue.

See below, and other posts in this thread.

> > If I ask Perl to open a file, and then include a directory that doesn't
> > currently exist within the file mame, will the directory be created?

> No, the directory must already exist.

Rats. Okay, no worries.

> First, including single quotes around $1 in the error message makes it
> obvious if there are any issues with leading or trailing whitespace such
> as newlines. And second, including $! in the error message will tell
> you why the open failed.

Yeah, using $1 ubstead of $! was a typo in the code. I've changed that,
and it now shows that it is indeed a permissions error, I'm just not sure
where to go from here.

Thanks,

Dave

-- 
Dave Stratford    ZFCA
http://daves.orpheusweb.co.uk/
Hexagon Systems Limited - Experts in VME systems development



------------------------------

Date: Fri, 16 Oct 2009 10:44:21 -0700 (PDT)
From: patrick <pgovern@u.washington.edu>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <5d5f01fe-eb7e-47ce-8e62-c8c93b6b570c@x25g2000prf.googlegroups.com>

On Oct 16, 9:36=A0am, Dave Stratford <da...@orpheusmail.co.uk> wrote:
> In article <hb9qs4$67...@pentheus.materna.de>,
> =A0 =A0Peter Tuente <peter.tue...@materna.de> wrote:
>
>
>
>
>
> > Dave Stratford schrieb:
> > > Hi folks,
>
> > > I'm not sure whether this is a Perl or a Linux issue.
>
> > > I'm developing CGI stuff, in perl, that will allow a user to upload a
> > > file from a web browser, which the perl script then places into
> > > specific directory.
>
> > > Most of it works, except that perl won't open the file. There's no
> > > useful error given, and I don't know which logs to look in, I've trie=
d
> > > the httpd logs, but they are empty for some reason.
>
> > > Is it possible that it is simply a linux permissions fault somewhere,
> > > and if so, how do I identify it?
>
> > > If I ask Perl to open a file, and then include a directory that doesn=
't
> > > currently exist within the file mame, will the directory be created?
>
> > > Any ideas gratefully received.
>
> > > Thanks,
>
> > > Dave
>
> > Hi Dave,
> > I think that there is a linux permission problem. The Web-User is
> > probably not allowed to create a file in the desired directory.
> > Try to change the access rights of the directory to writable for
> > everyone by issuing the following command as user 'dave':
> > =A0 =A0chmod 777 /home/dave
> > then change the open line to
> > =A0 =A0open ($fh,">","/home/dave/$modno-headshot$ext")
>
> I've set up a separate directory: /home/dave/UplodedPhotos changed the
> permissions as you suggested, (and the code to go with it!), but I'm stil=
l
> getting an error. I have worked out that it's a permissions error though.
>
> > .. and try again.
>
> Thanks,
>
> Dave
>
> --
> Dave Stratford =A0 =A0ZFCAhttp://daves.orpheusweb.co.uk/
> Hexagon Systems Limited - Experts in VME systems development- Hide quoted=
 text -
>
> - Show quoted text -

/home/dave/UplodedPhotos may be 777 but can
the public traverse thru /home/dave/ to get there.

Patrick


------------------------------

Date: Fri, 16 Oct 2009 14:16:20 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <hbada1$1e6$1@news.eternal-september.org>

Dave Stratford wrote:
> Hi folks,
> 
> I'm not sure whether this is a Perl or a Linux issue.
> 

>         open ($fh,">","~/$modno-headshot$ext")
>                              or die "can't create image file: $1";


'~' means the home directory of the agent running the script - in
this case the web server.

Apache on Linux is usually installed with home directory /var/www
which is not writeable by apache.

The directories accessible to apache must be specified in the
httpd.conf configuration file with a Directory option.


------------------------------

Date: Fri, 16 Oct 2009 14:35:10 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <slrnhdhhqt.ngb.tadmc@tadmc30.sbcglobal.net>

Dave Stratford <daves@orpheusmail.co.uk> wrote:
> In article <slrnhdgtpl.h2h.tadmc@tadmc30.sbcglobal.net>,
>    Tad J McClellan <tadmc@seesig.invalid> wrote:
>> Dave Stratford <daves@orpheusmail.co.uk> wrote:


>> > I've tried the httpd
>> > logs, but they are empty for some reason.
>
>
>> Errr, then where does the message you included below show up?
>
> In the web browser window, obviously.


How is that obvious?

It is not normal for STDERR messages to appear there...


>> > The relevant bit of the perl script looks like this:
>> >
>> >   my $phs   = param("phs");
>> >   my $img;
>> >   my $modno = param("modno");
>> >   my $valid = 1;
>> >   my $ext;
>> >   my $fh;
>> >
>> >   $phs = "" unless defined($phs);
>
>
>> You should declare variables when you first use them, rather than
>> all at the top.
>
> I did say relevant bit of code. 


The part that is relevant to seeing STDERR in the browser was not there...


> This is part of a subroutine.


That's nice, but you should still declare variables when you first use them.


>> >         read ($phs, $img, (stat ($phs))[7] == (stat ($phs))[7]);
>>                 ^^^^                         ^^
>>                 ^^^^                         ^^
>
>
>> WTF?
>
>> The first argument to read must be a filehandle. 
>
> $phs is a file handle. The second bit confuses me as well. I copied it


You should not cargo-cult code that you do not understand.


> from a bit of code that already works - that I didn't write.


Everything after the == (except the semicolon) can be deleted with
no change to how the program will execute.


>> $phs is a string, not a filehandle.
>
> Actually it's both.


Not in the code that was presented to us it wasn't...


>>     You should always enable warnings when developing Perl code!
>>     (ask for useful messages, part 1)
>
> Already have done, at the top of the code I already have:
> use CGI::Carp qw(fatalsToBrowser warningsToBrowser);


Well without seeing that code it is surely not obvious where you
saw messages when there was nothing in the error log.


>> There is no point in using a boolean operation if you are not
>> going to test the value of it somehow...


use PSI::ESP;

I expect that where you copied the code from DID test the value somehow
(which is much much better than not checking the return value).


    unless ( read $phs, my $img, -s $phs == -s $phs ) {
        die "read() failed: $!";
    }

or some such.


>> > The error I'm getting is : 'can't create image file:  at
>> > /var/www/cgi-bin/application.pl line 1816
>
>
>> Where are you "getting" that message from?
>
> The browser window, obviously.


How is that obvious?

It is not normal for STDERR messages to appear there...


> I've now changed the open to read :
>
>         $img = "/home/dave/UploadedPhotos/$mod_no-headshot$ext"
>         open ($fh,">",$img)
>              or die "can't create file  '$img' : '$!'";


Much nicer!


> I've created the UploadedPhotos directory, and done a chmod 777 on it.
>
> Still failing on the open, now with a permissions error:
>
> can't create file '/home/dave/UploadedPhotos/3082-headshot.jpeg' :
> 'Permission denied' at /var/www/cgi-bin/application.pl line 1820.


Now it is not a Perl problem, it is an OS problem.

Find out _why_ permission was denied.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Fri, 16 Oct 2009 14:37:05 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <slrnhdhhug.ngb.tadmc@tadmc30.sbcglobal.net>

Steve C <smallpond@juno.com> wrote:

> '~' means the home directory


Not in Perl.

Having tilde mean home directory is a feature of the shell not of Perl.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Fri, 16 Oct 2009 15:47:20 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <hbaikp$era$1@news.eternal-september.org>

Tad J McClellan wrote:
> Steve C <smallpond@juno.com> wrote:
> 
>> '~' means the home directory
> 
> 
> Not in Perl.
> 
> Having tilde mean home directory is a feature of the shell not of Perl.
> 
> 

good point.

perl -we 'open (F,">","~/foo")||die "nope"; print F "test"'
nope at -e line 1.


------------------------------

Date: Sat, 17 Oct 2009 14:39:25 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <slrnhdjent.m2u.hjp-usenet2@hrunkner.hjp.at>

On 2009-10-16 19:35, Tad J McClellan <tadmc@seesig.invalid> wrote:
> Dave Stratford <daves@orpheusmail.co.uk> wrote:
>> In article <slrnhdgtpl.h2h.tadmc@tadmc30.sbcglobal.net>,
>>    Tad J McClellan <tadmc@seesig.invalid> wrote:
>>> Dave Stratford <daves@orpheusmail.co.uk> wrote:
>>> >         read ($phs, $img, (stat ($phs))[7] == (stat ($phs))[7]);
>>>                 ^^^^                         ^^
>>>                 ^^^^                         ^^
>>
>>
>>> WTF?
>>
>>> The first argument to read must be a filehandle. 
>>
>> $phs is a file handle. The second bit confuses me as well. I copied it
>
>
> You should not cargo-cult code that you do not understand.
>
>
>> from a bit of code that already works - that I didn't write.
>
>
> Everything after the == (except the semicolon) can be deleted with
> no change to how the program will execute.

No. Firstly that would create a syntax error (the last parenthesis is
necessary, and you want to remove not only the stuff *after* the == but
the == as well), secondly it would change the semantics quite a bit.
Currently it's:

    read (
	$phs,
	$img,
	(stat ($phs))[7] == (stat ($phs))[7]
    );

which reads one byte if the size of the file stays the same between the
two calls to stat (very likely) and zero bytes if it changes. 

If you change it to:

    read (
	$phs,
	$img,
	(stat ($phs))[7]
    );

it will read the whole file.

(I think that the comparison was there to check that indeed the whole
file was read. Of course that belongs outside of the call and you have
to do something if it's not true ...)


>>> $phs is a string, not a filehandle.
>>
>> Actually it's both.
>
>
> Not in the code that was presented to us it wasn't...

In the code that was presented it wasn't anything because the code
wasn't complete and wouldn't run. However it was quite obvious that it
does use the CGI module, and the documentation of the CGI module says:

|       When the form is processed, you can retrieve the entered filename by
|       calling param():
|
|              $filename = param('uploaded_file');
[...]
|       The filename returned is also a file handle.  You can read the contents
|       of the file using standard Perl file reading calls:
|
|               # Read a text file and print it out
|               while (<$filename>) {
|                  print;
|               }


>
>>>     You should always enable warnings when developing Perl code!
>>>     (ask for useful messages, part 1)
>>
>> Already have done, at the top of the code I already have:
>> use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

And that's why you don't see anything in the logs. You explicitely told
CGI not to send errors to the log but to the browser ...


> use PSI::ESP;
>
> I expect that where you copied the code from DID test the value somehow
> (which is much much better than not checking the return value).
>
>
>     unless ( read $phs, my $img, -s $phs == -s $phs ) {
>         die "read() failed: $!";
>     }
>
> or some such.

Yup. I would add parentheses around the call to read here. Perl may know
the relative priority of a call to a builin function and ==, but I
don't.

	hp


------------------------------

Date: Sun, 18 Oct 2009 12:04:06 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <86eip181d5.fsf@gareth.avalon.lan>

Dave Stratford <daves@orpheusmail.co.uk> writes:


> Yeah, using $1 ubstead of $! was a typo in the code. I've changed that,
> and it now shows that it is indeed a permissions error, I'm just not sure
> where to go from here.
>
Do you have a shell on the webserver?

The best way to see what happens is to log in, change to whatever user
the webserver is running as, and then cd down to the directory you want
to write in and try to create a file.

Mart

-- 
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.


------------------------------

Date: Sun, 18 Oct 2009 14:30:47 -0700 (PDT)
From: Dave <daves@orpheusmail.co.uk>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <ef2b786d-b1d3-4e0a-9d51-821b67c2359c@k17g2000yqb.googlegroups.com>

On Oct 16, 6:44=A0pm, patrick <pgov...@u.washington.edu> wrote:
> On Oct 16, 9:36=A0am, Dave Stratford <da...@orpheusmail.co.uk> wrote:
>
>
>
> > In article <hb9qs4$67...@pentheus.materna.de>,
> > =A0 =A0Peter Tuente <peter.tue...@materna.de> wrote:
>
> > > Dave Stratford schrieb:
> > > > Hi folks,
>
> > > > I'm not sure whether this is a Perl or a Linux issue.
>
> > > > I'm developing CGI stuff, in perl, that will allow a user to upload=
 a
> > > > file from a web browser, which the perl script then places into
> > > > specific directory.
>
> > > > Most of it works, except that perl won't open the file. There's no
> > > > useful error given, and I don't know which logs to look in, I've tr=
ied
> > > > the httpd logs, but they are empty for some reason.
>
> > > > Is it possible that it is simply a linux permissions fault somewher=
e,
> > > > and if so, how do I identify it?
>
> > > > If I ask Perl to open a file, and then include a directory that doe=
sn't
> > > > currently exist within the file mame, will the directory be created=
?
>
> > > > Any ideas gratefully received.
>
> > > > Thanks,
>
> > > > Dave
>
> > > Hi Dave,
> > > I think that there is a linux permission problem. The Web-User is
> > > probably not allowed to create a file in the desired directory.
> > > Try to change the access rights of the directory to writable for
> > > everyone by issuing the following command as user 'dave':
> > > =A0 =A0chmod 777 /home/dave
> > > then change the open line to
> > > =A0 =A0open ($fh,">","/home/dave/$modno-headshot$ext")
>
> > I've set up a separate directory: /home/dave/UplodedPhotos changed the
> > permissions as you suggested, (and the code to go with it!), but I'm st=
ill
> > getting an error. I have worked out that it's a permissions error thoug=
h.
>
> > > .. and try again.
>
> > Thanks,
>
> > Dave
>
> > --
> > Dave Stratford =A0 =A0ZFCAhttp://daves.orpheusweb.co.uk/
> > Hexagon Systems Limited - Experts in VME systems development- Hide quot=
ed text -
>
> > - Show quoted text -
>
> /home/dave/UplodedPhotos may be 777 but can
> the public traverse thru /home/dave/ to get there.

Thank you, this gave me the biggest clue.

I've now set up the directory in /var/www/html/UploadedPhotos, done
the chmod 777 and it all now works as expected.

Thank you to everyone who responded.

Dave


------------------------------

Date: Sun, 18 Oct 2009 16:05:37 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <dn7nd5pqhq5nevgcfeiqjmu9925ifvtgjt@4ax.com>

Dave <daves@orpheusmail.co.uk> wrote:
>I've now set up the directory in /var/www/html/UploadedPhotos, done
>the chmod 777 and it all now works as expected.

Well, at least as long as nobody notices that hole. You do know that 777
opens a security hole as large as the galaxy, don't you?

jue


------------------------------

Date: Sun, 18 Oct 2009 20:11:16 -0400
From: sreservoir <sreservoir@gmail.com>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <hbgaqh$3n2$1@aioe.org>

Jürgen Exner wrote:
> Dave <daves@orpheusmail.co.uk> wrote:
>> I've now set up the directory in /var/www/html/UploadedPhotos, done
>> the chmod 777 and it all now works as expected.
> 
> Well, at least as long as nobody notices that hole. You do know that 777
> opens a security hole as large as the galaxy, don't you?

It shouldn't, unless someone has an account on the server.

But I generally prefer 770 with group daemon.

-- 

   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"


------------------------------

Date: Sun, 18 Oct 2009 17:39:44 -0700 (PDT)
From: Dave <daves@orpheusmail.co.uk>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <8eb23e17-fd37-4529-9547-052858af3374@b2g2000yqi.googlegroups.com>

On Oct 19, 1:11=A0am, sreservoir <sreserv...@gmail.com> wrote:
> J=FCrgen Exner wrote:
> > Dave <da...@orpheusmail.co.uk> wrote:
> >> I've now set up the directory in /var/www/html/UploadedPhotos, done
> >> the chmod 777 and it all now works as expected.
>
> > Well, at least as long as nobody notices that hole. You do know that 77=
7
> > opens a security hole as large as the galaxy, don't you?

Yeah.

> It shouldn't, unless someone has an account on the server.
>
> But I generally prefer 770 with group daemon.

Tried 770, but I got a server error. In fact I tried 666, 007 and a
number of other options. 007 worked for the write, but then I couldn't
read the files uploaded. Fairly obvious I guess, but it needed
testing. 666 didn't work, which isn't logical to me. I want to be able
to read and write image files, I don't want to execute anything, so
why do I need 777? Why wont 666 work? (Or 776? or 006?)

Dave


------------------------------

Date: Mon, 19 Oct 2009 01:58:06 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Can't open an output file from a perl CGI script.
Message-Id: <u5qsq6-g0f1.ln1@osiris.mauzo.dyndns.org>


Quoth Dave <daves@orpheusmail.co.uk>:
> On Oct 19, 1:11 am, sreservoir <sreserv...@gmail.com> wrote:
> > Jürgen Exner wrote:
> > > Dave <da...@orpheusmail.co.uk> wrote:
> > >> I've now set up the directory in /var/www/html/UploadedPhotos, done
> > >> the chmod 777 and it all now works as expected.
> >
> > > Well, at least as long as nobody notices that hole. You do know that 777
> > > opens a security hole as large as the galaxy, don't you?
> 
> Yeah.
> 
> > It shouldn't, unless someone has an account on the server.
> >
> > But I generally prefer 770 with group daemon.

If your webserver runs as group www or apache (as is common) you should
use that group, if possible.

> Tried 770, but I got a server error.

Then the dir was in the wrong group.

> In fact I tried 666, 007 and a
> number of other options. 007 worked for the write, but then I couldn't
> read the files uploaded. Fairly obvious I guess, but it needed
> testing. 666 didn't work, which isn't logical to me. I want to be able
> to read and write image files, I don't want to execute anything, so
> why do I need 777? Why wont 666 work? (Or 776? or 006?)

Learn about Unix permissions. You need execute permission on a directory
to use it as a component in a path; you need read permission to call
opendir/readdir. This is why directories usually have execute
permission.

Ben



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 2638
***************************************


home help back first fref pref prev next nref lref last post