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

I need a way to copy a file from point A to point B. I have tried to use File::Copy but for some reason my file didn't get moved.
my $location = "/home/name/public_html/header.gif"; my $temp_folder_path = "/home/name/public_html/test.gif"; copy("$location","$temp_folder_path\test.gif");
Perhaps what I'm trying to do in one step requires two? (copying the file over and renaming it).

Along with this I need your monkly advice. This is part of a file downloading script for a members section. It takes a list of files from the database and then writes a random file name for them. This is to prevent people from giving the actual URLs of our files out since the filenames will always be different they won't be able to.

By having a list of links to push and then have the script copy a file from one part of the server to the other seems a little costly. Especially if they have say 10MB files or greater. Then I think we're totally doing something wrong.

Our first idea was to copy the file they selected with a random name to a /temp/ folder. But again the problems we are having is 1) it doesn't seem to be working 2) the overhead of large files and 3) how would you delete the file once it's been opened? We want it there just long enough so they can view it on their screen but they can't reload it without going through the link process again.

Any and all advice on this would be very much appreciated.

Replies are listed 'Best First'.
Re: Copying files
by eyepopslikeamosquito (Archbishop) on Feb 14, 2005 at 22:15 UTC

    my $location = "/home/name/public_html/header.gif"; my $temp_folder_path = "/home/name/public_html/test.gif"; copy("$location","$temp_folder_path\test.gif");
    You've got a typo in the copy() line above, using \ instead of /. However, you also repeated test.gif twice. Finally, you should check the return code from copy to let perl tell you what is wrong. Something like this:
    my $location = "/home/name/public_html/header.gif"; my $temp_folder_path = "/home/name/public_html/test.gif"; copy($location, $temp_folder_path) or die "copy failed: $!";

Re: Copying files
by Animator (Hermit) on Feb 14, 2005 at 22:08 UTC

    You are talking about copying files, and a miunte later your post says: 'my file didn't get moved'... Make up your mind :)

    Your problem with the code you posted is that the \t in "$temp_folder_path\test.gif" is interpolated to a tab. Are you sure you don't need / or \\

    Update (after reading the entire post): Why do you insist on copying the files? Why not store the random file names (and the real file name) in some sort of database and open the real file (and send that one) when a person makes a request for a random file? (And after the request remove the random file from the DB)

      Okay, I meant "copy" :) And changing the \ to a / did fix that problem, thanks!

      Still need advice on everything else though if anyone has any suggestions.

      I don't think I quite understand what you mean. Can you try explaining your suggestion differently?

      If there is any way to do this without copying the files but also making it so the user NEVER knows the name of the original file, I'd love to hear it

      Thanks.

        Store the files where the webserver won't serve them, no matter how hard users try to guess their names.

        Generate a new unique key for each user who needs to access a file. Store this in a database of some sort that associates that particular key with the name of the file to serve. This can be a flat file (but watch out for locking!), a tied hash (fast and reasonably easy to set up), or a relational database (probably overkill unless you're already using one).

        Instead of redirecting the user to a temporary file with a randomly-generated name, redirect the user to another CGI program that takes in the unique key, looks up the appropriate file in the database, then serves the file to the user.

        Bonus tip: look into the Content-Disposition header to set the filename appropriately.