Hi bigup401,
In order to read some data from a file to a perl variable one may use open() and then read from the FILEHANDLE provided by open(). When you are done with it, you close that file, again using the FILEHANDLE which open() gave you when you called it.
***BUT*** If you want to rename a file, Perl can do this for you (just as the operating system does it for you (using, for example, the OS commands mv - for move - in Unix from the command line/prompt/terminal by giving it plain old FILENAMES). So, in this case you need to provide FILENAMES (and NOT filehandles).
Which means, you do not need to open() the file first to obtain a filehandle. What's more, you should not open that file in WRITING MODE (i.e the ">$FILE", note the > - it means OVERWRITE what's already in that file, and zero its contents to start with. Your file's previous contents are gone!)
Bottomline is: RENAME in Perl, operates with FILENAMES. NOT filehandles. So, do not open() a file first if you want to rename it. Just use one of the suggestions fellow Monks have already made. BUT NOTE: rename/delete/copy file operations using Perl require FILENAMEs and NOT filehandles. (of course there is another way to do it, but let's concentrate on this problem for now)
Now, there is a second issue here which holds down your learning curve from shooting upwards.
It is that you are confused as what RENAMES a file can do: YES you can rename a file so that it ends up in a different dir.
You do not need to first change its name in same dir and then move it to a different dir with a combination of open() and rename() commands. which eventually end up truncating (this is what open(DATA, ">hello"); does to the file "hello" if it exists: truncate = zero its contents.
Finally, reading your follow-up answers, I suspect that you are trying to get some data from client via your CGI script and save it to client-specified filename but you want it to be in another dir. In any event you want to specify a dirname along with a filename for the uploaded data to be saved do locally. Just go for it. open() dirname+filename for writing and save upload contents to it. This is valid, provided that dirname already exists and your script has writing permission. No fussing with rename() at all in this case.
In reply to Re: file handing
by bliako
in thread file handing
by bigup401
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |