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

I have an html form page and what I want to be able to do is take the filename that is entered in the textbox and move it to the directory of my choice. I know how to get the file name that is entered into the textbox, but once I have that how do I move the file. Could I use `cp $file /directory` since the backtick should tell it to do a file copy. Thanks for the help

Replies are listed 'Best First'.
Re: moving files
by flyfishin (Monk) on May 24, 2000 at 21:28 UTC
    What about using File::Copy? Assuming the web server has write permission in the directory where you want to place the file File::Copy should do what you want.
Re: moving files
by perlcgi (Hermit) on May 24, 2000 at 21:35 UTC
    Using an upload like this just add something basic like the following
    my $target = "/tmp/whatever"; open (OUTFILE, ">$target") or die "Could'nt open $target:$! \n"; print OUTFILE $file; close;
    If you are using NT or an OS that distinguishes binary mode from test files add binmode OUTFILE; after the open statement above.

      Using binmode is always good practice, regardless of the OS you're programming for - it has no overhead on systems that don't need it AFAIK

      Your proposed method of printing into the new file has the drawback (or the effect) that neither the original file time nor the original file attributes (as they are on the file system) get preserved.

      Sorry about the typo:
      an OS that distinguishes binary mode from test files
      should read
      an OS that distinguishes binary mode from text files.
Re: moving files
by httptech (Chaplain) on May 24, 2000 at 20:41 UTC
    Not sure what you want to do here exactly; are you wanting to have a user upload a file to the server through your script? Or do you just want to give them control to manipulate a file that is already on the server?
      The user will upload a file and my script will grab that filename, but from there how do I move the file.
RE: moving files
by ZZamboni (Curate) on May 24, 2000 at 23:38 UTC
    In Unix at least, the rename function is also used to move a file, although it cannot move a file between different file systems. For that, you would have to use one of the other suggestions posted.

    I don't know how rename behaves under NT or other operating systems.

    --ZZamboni

      Under Win32 variants (Win9x and Windows NT and Windows 2000), rename works a expected, that is, it can move within one file system.
Re: moving files
by lhoward (Vicar) on May 24, 2000 at 21:32 UTC
    Are you asking how you move the file? Or how you process a file uploaded via a FORM's <input type="file"...> tag?

    Or are you asking how to deal with a form in which a user enters a filename in one field and the file's contents in another (textarea) field?

    I guess I don't understand your question. Can you elaborate?