Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

opening filehandles, creating files

by madaket (Novice)
on Sep 07, 2003 at 20:28 UTC ( [id://289614]=perlquestion: print w/replies, xml ) Need Help??

madaket has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks,

I have modified an existing perl program (FormMail.pl) and am trying to create a new file and write to it. For example,

my $QB_file = $order_number . "SUF"; open (QBFILE, ">/home/client/www/data_dir/$QB_file") or die "could not + open QB output file: $!";
does not work ("no such file or directory").

This is what I have tried:

  1. taking out the -T switch
  2. commenting out use strict;
  3. untainting the $order_number (which comes from another file)
  4. putting the part of the filename path (/home/client/www/data_dir/) into a variable and concatenating it
and some other stuff, but nothing seems to work.

I would really like to create files with the order number as part of the filename and the FormMail.pl script works well.

Any suggestions?

Thanks.

Replies are listed 'Best First'.
•Re: opening filehandles, creating files
by merlyn (Sage) on Sep 07, 2003 at 21:10 UTC
      Listen to merlyn. I think I personally found
      some security holes in that script!
Re: opening filehandles, creating files
by simonm (Vicar) on Sep 07, 2003 at 20:37 UTC
    Make sure that the directory you're writing to already exists, or create it using mkdir. die "Directory is missing" unless ( -d '/home/client/www/data_dir' );

    Also note that if your web server is running chrooted, or mounts this same filesystem on another host, the directory path visible to your scripts might be different than the one you see when you log in to your shell or FTP account.

      simonm,

      The directory exists, but the perl program can not open the file (or create it) -- "no such file or directory"

      Another strange thing about this is that I can read and write to files that already exist, but I'm not able to create a new file or write to it.

      Thanks for your suggestion -- I put it in the script.

        If you are creating the file from inside FormMail.pl and you are trying to access FormMail.pl as a CGI script then you have to:
        1. find out what user the web server runs as
        2. become root
        3. chown the directory in question to that user
        4. chmod the directory accordingly (755 should do it)
        However, the error i was expecting was Permission denied instead of no such file or directory. One thing i always try is to see if that path really exists.
        ls /home/client/www/data_dir
        If it doesn't exist, problem solved. But if it does, then the error you have doesn't make sense. Perl will create the file if it is not found as long as the user running the script has permission. And please heed merlyn's advice and use NSM's FormMail instead (if you already aren't, of course ;)).

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: opening filehandles, creating files
by MarkM (Curate) on Sep 07, 2003 at 20:33 UTC

    Chances are, /home/client/www/data_dir is not the real path that you should be using, or, /home/client/www/data is the path on a machine other than the web server. In either case, the "no such file or directory" is quite clear that the executing script is not able to find one: /home, /home/client, /home/client/www, or /home/client/www/data_dir.

    If it was a permissions problem, you would get "permission denied." If it was a tainting problem, you would see a taint error from Perl.

Re: opening filehandles, creating files
by Abigail-II (Bishop) on Sep 07, 2003 at 21:53 UTC
    I've got to ask this. If you get an error message no such file or directory, what makes you do things like turning off tainting, turning off strictness, or the other things you list. What possible relationship can they have with no such file or directory?

    Abigail

      The filename is supposed to include a piece of data from another file -- that's why I was looking at taint, strict, etc.

      Progress is coming slowly -- right now, I am writing to a file, but the filename does not include the piece of data from another file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://289614]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found