in reply to Re: file handing
in thread file handing

now the problem i have. it copies when no rename but when i try to put rename. the file gets renamed but cant be copied after rename

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.";

Replies are listed 'Best First'.
Re^3: file handing
by poj (Abbot) on Sep 22, 2018 at 16:34 UTC

    see rename

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

      i have tried to figure it out. this is wat i mean that open function can be used instead file copy

      $IMG = $CGI->param("photo"); my $newfile = '09911.JPG'; my $copydir = 'img'; my $uploadfile = $CGI->upload($IMG); rename($uploadfile, $newfile); open ( UPLOADFILE, "+>$copydir/$newfile" ); close UPLOADFILE;

      now the code works as i like it. but i get my pics with zero bytes

        Hello bigup401,

        but i get my pics with zero bytes

        As johngg said below, you’re clobbering (i.e., truncating, deleting the contents of) the file by opening it for writing with >. From perlfaq5:

        How come when I open a file read-write it wipes it out?

        Because you're using something like this, which truncates the file then gives you read-write access:

        open my $fh, '+>', '/path/name'; # WRONG (almost always)

        Whoops. You should instead use this, which will fail if the file doesn't exist:

        open my $fh, '+<', '/path/name'; # open for update

        Using ">" always clobbers or creates. Using "<" never does either. The "+" doesn't change this.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,