Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploaded file size is 0 2

Status
Not open for further replies.

gixxerrider

Technical User
Jul 22, 2003
2
US
hopefully someone can help, everytime I upload the file size is always 0. below is the script.

my form does have the ENCTYPE="multipart/form-data" in the form action
Thanks for any help.

#$upload_dir is directory to upload to
#$xfile = $input{'file'}; file field on form
#$filename is the actual file name such as image.jpg after the path to the directory has been stripped off.




sub Upload {

open (OUTFILE, ">$upload_dir$filename");
while (my $bytesread = read($filename, my $buffer, 1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);

#end of Upload
}
 
Have a look at the FAQ section here, look for uploading files towards the end

HTH
--Paul
 
thanks for the reply but that didn't help. file size is always 0
 
Hi gixxerrider,
Did you ever figure this out? From time to time this starts mysteriously happening to all my uploading scripts on a particular server. And it can mysteriously go away too.

I am wondering if I need to build in some kind of wait or try-again. Anybody else who has an idea please join in.

I am no Perl expert. I learned just enough a few years ago to write some build-your-own-website tools which work still fine except when this starts happening.

If anyone can help I can be much more specific about where I think the problem is.
Droosan
 
Isn't this something to do with the way you are opening the file, as in clobbering existing content ready for rewriting.

You have used;

open(FILE, ">$file");

IF you want to update without erasing it first try;

open(FILE, "+>$file");

Have a good look at perlopen.
 
Thanks for the reply.

I do want it to clobber what is already there, if anything. I don't think this is the issue because it occurs whether or not there is already such a named file in the folder.

The file opens properly and creates no error. However the while condition

Code:
read($filename, my $buffer, 1024)

is not true, even once, and nothing is written to the file.

Thanks again,
Keep the ideas coming.
 
I think the problem is that you're reading the variable $filename and not the CGI parameter that was passed from the client.

For some clients, this isn't a problem (Mozilla, Firefox I know of), but for IE, it does cause a problem as the parameter passed contains the full path and name of the file being uploaded, hence the need to strip that information out when saving the file.

Code:
open(SAVE, ">$uploaddir$filename") or die "Can't open file: $!";

binmode SAVE;

while(my $size = read($input{'file'}, my $data, 1024))
{
	print SAVE $data;
	my $total_size += $size;
}
close(SAVE);

Hope this helps!

- Rieekan
 
Here's a snippet of code I used when uploading files, and like Rieekan said, you have to strip out the unnecessary information before playing with it. Mine's a little more long winded but seems to work. Setting the content-type for each cgi script is also important, but then you probably already new that.
Code:
print "Content-type: text/html\n\n"

my $upload_dir = "../uploads";
my $query = new CGI; 
my $filename = $query->param("cv"); 

if ($filename ne "")  { 
	$filename =~ s/.*[\/\\](.*)/$1/; 
	my $upload_filehandle = $query->upload("cv"); 
	
	open UPLOADFILE, ">$upload_dir/$filename"; 
	
	while ( <$upload_filehandle> )  { 
	   	print UPLOADFILE; 
	} 
	
	close UPLOADFILE;
}

Rob Waite
<a href=" Ltd</a>
 
Now, the problem has gone away. This is bad news because I can't test to see if your suggestions are improvements. The problem may show up tomorrow or next year.

Thanks, Waiterm and Rieekan, for your suggestions, I will mess around with them and see if they help my understanding. The problem seems regardless of client's browser, or platform, for that matter.

If anyone else has ideas let me know. The problem comes and goes and whatever is changing must be at the host or very near it.

Again the problem manifests itself when:
Code:
read($file, $data, 1024)
doesn't produce anything.
 
One way you could test for this is to do the following:

Code:
my $total_size;

while(my $size = read($input{'file'}, my $data, 1024))
{
    print SAVE $data;
    $total_size += $size;
    if ($total_size == 0)
    {
        #Send error to log
        #send emailed error to webmaster
        #kill out of script
    }
}

Of course, hopefully when you kill the script you're sending the user to some kind of error page letting them know what's going on as well.

Hope this helps!

- Rieekan
 
I do spit back to the user to email me, it is a problem I am aware of but haven't figured out yet.

However I hadn't thought of the script emailing me. I'll have to think about that since I don't need an email for every single unsuccessful upload.
 
Hopefully someone can point me in the right direction. I am trying to get a perl script to take a file upload for some processing and am at my wit's end.

I've searched here and on google and have tried so many of the examples available I'm just getting myself even more confused. I've been trying methods both with and without use of CGI.pm with similar results.

The problem is that I can only seem to get a filename out of the file form field. I have it set to Post action and the enctype is multipart/form, so I'm sure the form is coded correctly; in fact I also have a textarea field that is working correctly. My problem is I cannot seem to get the actual file content passed to the server. The script will create an empty file with the correct filename, and I can even hard-code some text that gets written, but that's all.

Is there something within a perl script that would cause this, or could it be a server configuration issue? If it's the server, any guidance as to how to fix that? TIA
 
gixxerrider,

Are you using the "use strict;" directive?
I think you'll find your problems are caused by putting the "my $buffer;" in your read function call.

I suggest that if you don't have "use strict;" then you add it and re-test. You may find an error on the print line when you do.

To fix, try chainging your code to this:

Code:
sub Upload {

open (OUTFILE, ">$upload_dir$filename");
my $buffer;
while (my $bytesread = read($filename, $buffer, 1024)) {
  print OUTFILE $buffer;
}
close (OUTFILE);

#end of Upload
}

And see if that solves the problem.



Trojan (also a gixxer (gsx-r1000) rider!!).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top