Gossamer Forum
Home : General : Perl Programming :

how to transfer image from one dir to another?

Quote Reply
how to transfer image from one dir to another?
I am hoping someone knows how to move an image file from one directory to another. This will be used in the validation/approval process, where the successful submission (uploaded file) will be transferred from a validation directory to the "real" directory. I am using the lowly Links2, but the process should be similar for most of the GT scripts. So far I have the image getting from the add form, through an upload validation sub (size, type, etc), and into the holding area. The image shows up in the validate/modify forms, so I can see what they uploaded.

Now I need to do the more complicated stuff, like unlinking if denied (easy enough), and moving the approved images to the correct location (harder). There is a field in the .db for the file name, so I don't have to keep track of extension variables (png, gif, jpg). Any help would be greatly appreciated!


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
Hi,

Have you tried File::Copy ?

What I would do, is maybe try with the simpler option first:

Code:
`mv /old/path.gif /new/path.gif`;

..or system(qq|mv /old/path.gif /new/path.gif|)

Otherwise, you could look at using File::Copy I guess.

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] how to transfer image from one dir to another? In reply to
You should use File::Copy before resorting to icky system calls which are not portable, i.e. it will not work on Windows!.

Last edited by:

Wychwood: Nov 17, 2009, 7:05 AM
Quote Reply
Re: [Wychwood] how to transfer image from one dir to another? In reply to
Ok, working on that suggestion, thanks.

I put use File::Copy; at the start (below Eval code) in admin.cgi, and in db.pl, sub verify, I used this:

$original_file = "$SAVE_DIRECTORY/validate/$in{'Filename'}";
$new_file = "$SAVE_DIRECTORY/$in{'Filename'}";
move($original_file, $new_file) or die "Move failed: $!";


I got this: Error Message : fatal error: Move failed: Invalid argument at /path/to/links/cgi-bin/photo_upload/admin/db.pl line 710. (changed the path...). The move($original_file etc is line 710.

So then I changed the variables to absolute paths, and tried to move a specific file...

move("/path/to/links/cgi-bin/photo_upload/pics/validate/10.jpg", "/path/to/links/cgi-bin/photo_upload/pics/10.jpg") or die "Move failed: $!";

but it did not work... (Line number changed due to added code.)

Error Message : fatal error: Move failed: No such file or directory at /path/to/links/cgi-bin/photo_upload/admin/db.pl line 712.

Yes, the file is there.
Yes, File::Copy is on the server. Any thoughts?

If I don't put in the die statement, admin.cgi executes as normal.


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
MMm, this should work:

move("/path/to/links/cgi-bin/photo_upload/pics/validate/10.jpg", "/path/to/links/cgi-bin/photo_upload/pics/10.jpg") or die "Move failed: $!";

If you remove the "or die", does the file actually get moved?

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] how to transfer image from one dir to another? In reply to
No, it did not move. Will continue experimenting...


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
What permissions do the feiles have? Also, who are they "owned" by#? (i.e "root", "apache", or "user" ?)

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] how to transfer image from one dir to another? In reply to
The directory is 755, the file is 666, not sure how to determine ownership, but I suspect 'user'.


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
What happens if you do it in just a basic test.pl script?

Code:
#!/usr/bin/perl

print "Content-Type: text/html \n\n";
use File::Copy;

move("/path/to/links/cgi-bin/photo_upload/pics/validate/10.jpg", "/path/to/links/cgi-bin/photo_upload/pics/10.jpg") or die "Move failed: $!";

print "Done...";

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] how to transfer image from one dir to another? In reply to
As I made the test file, it jumped out at me that I had the wrong directory: cgi-bin instead of www! Blush
So if I use the absolute path, and name the file to be moved, it works, but using a variable (any variable), it does not.

Not working:
$original_file_path = "$SAVE_DIRECTORY/validate/$in{'Filename'}";
$new_file_path = "$SAVE_DIRECTORY/$in{'Filename'}";
move($original_file_path, $new_file_path) or die "Move failed: $!";

Not working:
move("/path/to/links/www/photo_upload/pics/validate/$in{'Filename'}", "/path/to/links/www/photo_upload/pics/$in{'Filename'}") or die "Move failed: $!";

No error (validates new link), not working (no pic move), renames validate directory to }:
my $file_name= "$in{'Filename'}";
move("/path/to/links/www/photo_upload/pics/validate/$file_name", "/path/to/links/xvmcoc/www/photo_upload/pics/$file_name}") or die "Move failed: $!";

Same result if quotes removed:
my $file_name= $in{'Filename'};

or if my is removed.



Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
Apparently the problem is in getting the variable, which is not happening correctly. If I specify what $file_name is (31.jpg), then it moves the file correctly. At least now I see where further investiagation is needed. The image name is stored in the field Filename, so shouln't this work? The code works in that location if I specify the file name...

In Links2, db.pl, sub verify:

# Now let's see if we have something to add to the real database, then
# let's do it.
if ($rec_to_validate) {
open (DB, ">>$db_file_name") or &cgierr("error in validate_records, unable to open db file: $db_file_name. Reason: $!");
flock(DB, 2) if ($db_use_flock);

foreach $id (keys %validate_list) {
if ($validate_list{$id} == 2) {
print DB &join_encode(%{$links{$id}});
$validate_list{$id} = 0;
}
}
close DB;
# >>> Image Upload mod 4 (next 2 lines) (add.cgi)
$file_name= "$in{'Filename'}";

move ("$SAVE_DIRECTORY/validate/$file_name", "$SAVE_DIRECTORY/$file_name") or die "Move failed: $!";
# <<<

}


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
Hi,

Assuming all the variables are right, it should work fine. You tried doing a "print" of the path its trying to access?

Been a while since I worked on Links2, so afraid I can't help you any more than that :(

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] how to transfer image from one dir to another? In reply to
The ony variable available right there is $id, so I need to access a hash and get the rest of the link info, I guess...


Leonard
aka PerlFlunkie
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
This works! Smile Had to look in the correct directory... Must be suffering from code blindness. Cool


Code:
opendir (GRAPHIC, "$SAVE_DIRECTORY/validate") or &cgierr("unable to open directory in delete records: $SAVE_DIRECTORY. Reason: $!");
@files = readdir(GRAPHIC);
closedir (GRAPHIC);
$file_test = $id . ".";
foreach $file (@files) {
if ($file =~ /^$file_test/) {
move("$SAVE_DIRECTORY/validate/$file", "$SAVE_DIRECTORY/$file") or die "Move failed: $!";
}
}


Leonard
aka PerlFlunkie

Last edited by:

PerlFlunkie: Nov 18, 2009, 4:16 PM
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
haha I've suffered from that more than my fair share of times ;) Glad its working now though.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [PerlFlunkie] how to transfer image from one dir to another? In reply to
I don't think you can put $in{'Filename'} inside a quoted string. It won't interpolate the variable. You need to remove the single quotes or assign the value to another variable and use that, eg:

my $file = $in{'Filename'};

Last edited by:

Wychwood: Nov 19, 2009, 5:00 AM
Quote Reply
Re: [Wychwood] how to transfer image from one dir to another? In reply to
Andy, thanks, and I know you have had the same affliction! Pirate

Wychwood, that has bben a problem , and I have solved it before as you suggested. It's not easy trying to keep the code simple...


Leonard
aka PerlFlunkie