CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
File upload: save under same name?

 



ReLaX99A
Deleted

May 30, 2000, 7:05 AM

Post #1 of 7 (652 views)
File upload: save under same name? Can't Post

I'm having troubles getting this file uploading script to work. I'm trying to save the uploaded file in my c:\temp directory. It does save it but under some temporary name, like CGItemp238001. And what I am trying to do is to save it under the exactly the same name as the original file. What do I do wrong or forget to do???

My script is below. (I'm running the script under Windows NT)
Thanks!!!

use CGI qw/:standard/;
$CGI::POST_MAX=1024*1024; #max 1MB files
$file_name = param('my_file');
&Print_Error if ($file_name eq "");
&Upload_File;

sub Upload_File{
open (SOURCE, "$file_name") or die "$!";
open (TARGET,">c:\Temp\$file_name") or die "$!";
binmode(SOURCE);
binmode(TARGET);
while ($bytesread=read(SOURCE,$buffer,1024))
{
print TARGET $buffer;
} #while
close OUTFILE;
close TARGET;
} #end of sub

sub Print_Error{
print "Content-type: text/html\n\n";
print "<p>Field 'File Name' must be filled in for this script to work properly.</p>\n";
exit(0);
} #end of sub


ReLaX99A
Deleted

May 29, 2000, 10:40 PM

Post #2 of 7 (652 views)
Re: File upload: save under same name? [In reply to] Can't Post

Yeah, the script works now. Thanks a lot!!!

I have another question though and I would really appriciate if I could get an answer.

Right now when I upload a file using my script, I also get CGItempxxxxxxx files (where x is a digit) in my c:\Temp directory. Each of those temporary files takes up as much space as the file I was uploading. Is there any way to avoid storing these temporary files? Or would I have to manually delete them every now and then?


CuzDesign
Deleted

May 29, 2000, 10:55 PM

Post #3 of 7 (652 views)
Re: File upload: save under same name? [In reply to] Can't Post

With the code I gave you should handle
uploading the file for you without adding any
temp file names or temp files.

By looking at your code it looks as though
you are telling it to write 2 files instead
of one:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open (SOURCE, "$file_name") or die "$!";
open (TARGET,">c:\Temp\$file_name") or die "$!";
binmode(SOURCE);
binmode(TARGET);
</pre><HR></BLOCKQUOTE>

Try taking out the TARGET file and only useing the other one.
Like so:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub Upload_File{
open (SOURCE, "$file_name") or die "$!";
binmode(SOURCE);
while ($bytesread=read(SOURCE,$buffer,1024))
{
print SOURCE $buffer;
} #while
close OUTFILE;
} #end of sub
</pre><HR></BLOCKQUOTE>

Hope this helps.

Regards,
Ray



CuzDesign
Deleted

May 30, 2000, 8:46 AM

Post #4 of 7 (652 views)
Re: File upload: save under same name? [In reply to] Can't Post

You need to strip the ellements off the begining
of the file input filed first then assign the
name of the file to a variable. Heere is
how I recently did it.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$basedir = '/path/to/image/dir;

$allowall = "yes";
sub upload {
$onnum = 1;

while ($onnum != 11) {
my $req = new CGI;
my $file = $req->param("FILE$onnum");
if ($file ne "") {
my $fileName = $file;
$fileName =~ s!^.*(\\|\/)!!;
$newmain = $fileName;
if ($allowall ne "yes") {
if (lc(substr($newmain,length($newmain) - 4,4)) ne $theext){
$filenotgood = "yes";
}
}
if ($filenotgood ne "yes") {
open (OUTFILE, ">$basedir/$fileName");
while (my $bytesread = read($file, my $buffer, 1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
}
}
$onnum++;
}
}
</pre><HR></BLOCKQUOTE>

Hope this helps.

The $basdir is a global variable on mine cause I
use it foe other methods.

Regards,
Ray


ReLaX99A
Deleted

May 30, 2000, 11:21 AM

Post #5 of 7 (652 views)
Re: File upload: save under same name? [In reply to] Can't Post

Well, I did modify my code as you suggested, but it still stores those temporary files.

For example, when I upload text file "aaa.txt". I get "aaa.txt" in my Upload folder, and in addition I get two more files in my c:\Temp folder. One of those files is named "CGItemp520001" and another is "nsform21.TMP" The content of CGItemp520001 is exactly the same as "aaa.txt", and below is what is in the other file:

Content-type: multipart/form-data; boundary=---------------------------6406137692106
Content-Length: 398

-----------------------------6406137692106
Content-Disposition: form-data; name="my_file"; filename="C:\WINNT\Profiles\ecpjm\Desktop\aaa.txt"
Content-Type: text/plain

*Hello. This is a sample text file, in which there's one line!!!*
-----------------------------6406137692106
Content-Disposition: form-data; name="submit button"

Upload File
-----------------------------6406137692106--


I really don't think my code does that but I have no idea what is happening then...

Here's my code:

#!perl
use CGI qw/:standard/;
$CGI::POST_MAX=1024*1024; #max 1MB files
my $file = param("my_file");
&Print_Error if ($file eq "");
#&Print_Results;
&Upload_File;

sub Upload_File{

print "Content-type: text/html\n\n";

my $file_name = $file;
$file_name =~ s!^.*(\\|\/)!!;
open (TARGET,">/InetPub/wwwroot/Uploads/$file_name") or die "$!";
binmode(TARGET);

while ($bytesread=read($file,$buffer,1024))
{
print TARGET $buffer;
} #while

close TARGET;

print "<p>FILE IS UPLOADED!</p>\n";
exit(0);
} #end of sub

sub Print_Error{
print "Content-type: text/html\n\n";
print "<p>Field 'File Name' must be filled in for this script to work properly.</p>\n";
exit(0);
} #end of sub


japhy
Enthusiast

May 30, 2000, 8:41 PM

Post #6 of 7 (652 views)
Re: File upload: save under same name? [In reply to] Can't Post

Mental note to everyone on windows... doing things like:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open FILE, "c:\testfile\action.txt";
</pre><HR></BLOCKQUOTE>

is A Bad Thing. In double-quoted strings, \t is a tab, and \a is an alarm. You don't want that. Please use forward slashes. Please.

For more info, read my article from March, Quoting Operators and Interpolation.


ReLaX99A
Deleted

May 31, 2000, 4:03 AM

Post #7 of 7 (652 views)
Re: File upload: save under same name? [In reply to] Can't Post

Yes. That is definitely a bad thing Smile Your code is just not going to work if you do that.

Anyways, anyone has any ideas about my question that I asked above? (Why do I get those temporary files when I do uploading?)

Thanks!!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives