Re: file handing
by johngg (Canon) on Sep 22, 2018 at 14:56 UTC
|
my $openfile = open(DATA, ">$FILE"); #OPEN FILE
Don't try running that code as you are opening the JPEG file for writing and will therefore clobber the contents, probably not what you want. Instead of trying to roll your own file copying/moving code you might be safer following poj's original advice. If you are doing this as a learning exercise then make good back-ups, read the tutorial poj mentions (and the reference doc) and practice on copies of your files to avoid disappointment.
| [reply] [d/l] |
Re: file handing
by poj (Abbot) on Sep 22, 2018 at 13:37 UTC
|
| [reply] |
|
|
use strict;
use warnings;
use File::Copy;
my $NFILE = "09911";
my $FILE = '02190.JPG';
my $filetobecopied = $FILE;
my $foldertocopyto = "img";
my $rename = rename($FILE, "$NFILE.JPG");
copy($rename, $foldertocopyto) or die "File cannot be copied.";
| [reply] [d/l] |
|
|
rename OLDNAME,NEWNAME
Changes the name of a file; an existing file NEWNAME will be clobbered. Returns true for success, false otherwise.
poj | [reply] |
|
|
|
|
|
|
| [reply] |
|
|
| [reply] |
Re: file handing
by lyklev (Pilgrim) on Sep 22, 2018 at 21:29 UTC
|
Not trying to be mean, but you are quite clueless about opening files, how the commands work, and how to use Perl effectively.
I can strongly recommend “Learning Perl”. It taught me a lot, was fun to work with, and made me a better programmer.
| [reply] |
Re: file handing
by bliako (Abbot) on Sep 23, 2018 at 23:11 UTC
|
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.
| [reply] [d/l] [select] |
Re: file handing
by Anonymous Monk on Sep 23, 2018 at 08:11 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
my $OLDFILE = '02190.JPG';
my $NEWFILE = 'img/09911.JPG';
open my $fh, '<', $OLDFILE;
binmode $fh;
my $data = join '', <$fh>;
close $fh;
open $fh, '>', $NEWFILE;
binmode $fh;
print $fh $data;
close $fh;
print "Copied $OLDFILE to $NEWFILE\n";
| [reply] [d/l] |
|
|
thanks but i know file handling. the only problem i have am trying to play around it for more knowledge
the problem. is now getting file with zero byte. the debt was someone telling me u cant rename, write, read and check image extension with built in funcation open() that i have to use module like image magick bra bra bra. but i know i can archive this without any single module using only built in function open(). and so far here is my code works well the problem i have now is the file uploaded in zero byte. that means cant open
my $photo = $CGI->param("pic")
my $copydir = 'img';
my ($file_extension) = $photo =~ /([^.]+)$/; #get photo extension
my $newf = "011190.$file_extension"; #rename photo to its exte
+nsion to keep the original resolutions of the file
rename($photo, $newf);
$upload = $CGI->upload("pic");
open ( UPLOADFILE, ">$copydir/$newf");
close UPLOADFILE;
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
You forgot to print $upload to UPLOADFILE:
$upload = $CGI->upload("pic");
open UPLOADFILE, ">", "$copydir/$newf";
binmode UPLOADFILE;
print UPLOADFILE $upload; # YOU FORGOT THIS! :-)
close UPLOADFILE;
Also try these lines at the top of your script
to more easily spot any errors:
use autodie;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
| [reply] [d/l] [select] |
|
|
|
|