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

How can I move any type of file (text, sound, image, etc) from one folder on my website to another? I have a company hosting my site if that makes any difference, so it's not my computer I'm running it on.

For an example I want the image

/home/fred/public_html/temp/image.gif
To be moved to
/home/fred/public_html/final/image.gif
This has to be done via CGI, so please don't give me a command-line or shell line to do it with. I want 2 stages of files for my script, so when the admin approves of it it'll be moved from folder1 to folder2. Otherwise it'll stay and rott so no one on the site sees it.

thank you everyone

Replies are listed 'Best First'.
Re: moving files from folder1 to folder2
by haoess (Curate) on May 14, 2004 at 22:41 UTC

    Sorry, I don't see any way to rename files via CGI. Do you mean perl? Then you should have a look at the mentioned rename function. rename has some limits. Maybe File::NCopy can help you.

    -- Frank

      Why are you two talking about renaming the file? Am I not understanding what I'm asking? Is that what you'd need to do in order to transfer the file from one directory into another?

      Would this be what I need?

      use File::NCopy; # the below are the default config values $file = File::NCopy->new( 'recursive' => 0, 'preserve' => 0, 'follow_links' => 0, 'force_write' => 0, 'set_permission' => \&File::NCopy::u_chmod, 'file_check' => \&File::NCopy::f_check, 'set_times' => \&File::NCopy::s_times, ); $file = File::NCopy->new(recursive => 1); $file->copy "/home/fred/public_html/temp/image.gif","/home/fred/pu +blic_html/final/image.gif";
        Why are you two talking about renaming the file? Am I not understanding what I'm asking? Is that what you'd need to do in order to transfer the file from one directory into another?
        Oh, come on, show some initiative. Read the manual page, and learn what rename is about, and then try it out.

        Abigail

        What is your difference between moving and renaming? In UNIX language, you can rename a file if source and destination are in the same filesystem (aka partition). That's very fast, because only inode information is changed. If they are not: You copy the source file to destination and unlink the source file. That's the way it goes.

        If you use perl's rename, you should take care about this. That's why I pointed you to the CPAN module. It takes this care for you and does the same as mv(1) does (if renaming is not possible): Read the source file and write it to its destination.

        -- Frank

Re: moving files from folder1 to folder2
by saskaqueer (Friar) on May 15, 2004 at 07:24 UTC

    Moving a file is effectively the same as renaming a file. Perhaps slightly different point of view on the user's side of things, but to the machine, moving and renaming are one and the same.

    Excerpt from perldoc -f rename:

    rename OLDNAME,NEWNAME Changes the name of a file; an existing file NEWNAME will be clobbered. Returns true for success, false otherwise.

    This means you can do something like this in your code:

    rename( '/home/fred/public_html/temp/image.gif', '/home/fred/public_html/final/image.gif' ) or die( "rename failed: $!" );

      But rename does not work across filesystems (at least under linux).

      Update corrected typo: s/remove/rename/

Re: moving files from folder1 to folder2
by Abigail-II (Bishop) on May 14, 2004 at 22:28 UTC
    rename

    Abigail

Re: moving files from folder1 to folder2
by Anonymous Monk on May 14, 2004 at 23:27 UTC
    I use this code to upload files via CGI, I know this works.
    my $mode = "0755"; my $localfile = "$uploaddir/$filename"; # open a new file and transfer bit by bit from what's in the buffe +r open( SAVED, ">>$localfile" ); while ( my $bytesread = read( $remotefile, my $buffer, 1024 ) ) { print SAVED $buffer; } close SAVED; chmod $mode, "$localfile"; # or die "can't chmod: $!";
    Do you think this will work for what the OP wanted? It tried hacking it to open two files and read from the original and write to the new one.
    my $mode = "0755"; my $localfile = "$uploaddir/$filename"; my $newfile = "$uploaddir2/$filename"; # open original file open( SAVED, ">>$localfile" ); # open new file open( NEW, ">$newfile" ); # write the original file to the new filename while ( my $bytesread = read( $remotefile, my $buffer, 1024 ) ) { print NEW $buffer; } close(SAVED); close(NEW); chmod $mode, "$newfile"; # or die "can't chmod: $!";
      "<castaway> just reply and say 'that was me'"

      This node was mine, by the way :) "that was me"! :)



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid