Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Moving a .html file

by Sam_07 (Initiate)
on Feb 25, 2012 at 02:52 UTC ( [id://956044]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that creates a webpage (its content & name are be based on what was selected in the previous form). The problem I am having is that the script refuses to create the file in a separate directory, no matter how I try. I have resorted to moving the file once it is created, but that isn't working either. I have left my attempts in the code as comments so that you can see what I have already tried. Any help is appreciated, even if you see something that isn't related that could be improved!
#!/usr/bin/perl -w use warnings; use CGI; #use File::Copy; my($buffer) = ""; my($firstChoice) = ""; my($secondChoice) = ""; my(@values) = ""; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); (@values) = split(/&/, $buffer); $firstChoice = $values[0]; $secondChoice = $values[1]; ($NULL, $firstChoice) = split(/=/, $firstChoice); ($NULL, $secondChoice) = split(/=/, $secondChoice); #print "Content-type: text/plain\n\n"; #print "$firstChoice\n"; #print "$secondChoice"; my($dynamic) = "$firstChoice$secondChoice.html"; open (FH, "+>" . $dynamic); print FH "<html>\n"; print FH " <head>\n"; print FH " <title>$firstChoice and $secondChoice</title>\n"; print FH " </head>\n"; print FH " <body>\n"; print FH " You chose $firstChoice and $secondChoice\n"; print FH " </body>\n"; print FH "</html>\n"; close (FH); #These are all of the ways that I have tried to move the file and fail +ed: #1: #rename "$dynamic", "dynamics/$dynamic"; #2: #system("mv $dynamic dynamics/"); #3: #my($original_file) = "$dynamic"; #my($new_file) = "dynamics/$dynamic"; #move($original_file, $new_file); print "Location: dynamics/$dynamic\n\n";

Replies are listed 'Best First'.
Re: Moving a .html file
by GrandFather (Saint) on Feb 25, 2012 at 03:58 UTC

    oko1 has suggested the most likely reason your script is failing, however you'd get to that answer a lot quicker if you added some basic sanity checking to your script. For example your open would be better as:

    open my $fout, '>', $dynamic or die "Failed to open >$dynamic<: $!\n";

    Note the use of a lexical file handle, three parameter open, reporting of failure of the open (using the "or die" - that's the most important part) and a simple create ('>') instead of the original append which probably doesn't do what you want according to your description.

    Actually there is a lot in that code that could be improved. Consider:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $buffer; read STDIN, $buffer, $ENV{'CONTENT_LENGTH'}; my ($firstChoice, $secondChoice) = split /&/, $buffer; s/[^=]*=// for $firstChoice, $secondChoice; my $fileName = "$firstChoice$secondChoice.html"; open my $fOut, '>', $fileName or die "Failed to open >$fileName<: $!\n +"; print $fOut <<HTML; <html> <head> <title>$firstChoice and $secondChoice</title> </head> <body> You chose $firstChoice and $secondChoice </body> </html> HTML close $fOut; print "Location: dynamics/$fileName\n\n";

    Oh, in case it's not obvious I've not tested this code. ;)

    True laziness is hard work
Re: Moving a .html file
by oko1 (Deacon) on Feb 25, 2012 at 03:16 UTC

    Check the ownership/permissions of the directory you're trying to move it into. If the account under which your webserver is running isn't allowed to write to it, then you've found your answer.

    -- 
    I hate storms, but calms undermine my spirits.
     -- Bernard Moitessier, "The Long Way"
Re: Moving a .html file
by bitingduck (Chaplain) on Feb 25, 2012 at 03:31 UTC

    I stripped out your front matter (everything from "my ($buffer)up to the commented out prints of your first and second choices) and ran it with fixed values ("fred" and "bob") for $firstChoice and $secondChoice and uncommented your second try (the "mv" command), and it worked fine, once I made the dynamics directory.

    So if you uncomment your debug code to print the $firstChoice and $secondChoice and there's something in them, you should get something that works. Have you checked that you can create files manually in the directory where you want to create them?

    Why isn't yours working? My first guess would be a permissions problem for the destination directory. Do you need to do a mkdir to create the destination directory (I didn't do that before the first time I ran your code and got an error back)

Re: Moving a .html file
by Sam_07 (Initiate) on Feb 25, 2012 at 05:48 UTC
    It turned out that the permission for the dynamics folder was 755, it needed to be 775. After that the 2nd method worked just fine! Thanks for all the help and improvement suggestions! As far as using ">" to open instead of "+>", the page will be changed in some way almost every time this script is called so the whole page has to be re-written.

      So '>' is almost certainly what you want rather than '+>' which opens the file for read/write, given that your script doesn't read from the file. From the open documentation:

      You can put a '+' in front of the '>' or '<' to indicate that you want both read and write access to the file
      True laziness is hard work
        Ahh I see; I was under the impression that +> opened & truncated. Thanks yet again!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found