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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file size not transferring in uploading a file (browse feature)

Status
Not open for further replies.

there

Programmer
Feb 4, 2002
15
CA
Dear wise-one,

I have been trying to get the upload file feature to work. It is almost working but one problem, it is not transferring the file size. the file is transfer in the right location and file name on the web server but the file size is 0.

another question "binmode" is this to make sure the file is tranferring in binary mode?

here is the code below
----------------------
##--------
# uploadImage()
##--------
sub uploadImage
{

$upfile = lc(join '', split / /,(split/\\/, param("logo"))[-1]);

$abs_file_to_create .= "/location_on_the_web_server/";
$abs_file_to_create .= "$upfile";

open(NEW_FILE,">$abs_file_to_create") || die "Can't open $abs_file_to_create for output: $!";
binmode NEW_FILE;
binmode $upfile;
while ($size = read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
}
close NEW_FILE;

}

I would love your advise. thanks Thank you for your knowledge, wisdom and time!

There
 
while ($size = read($upload_logo, $data, 8192))

Should $upload_logo be $upfile instead? Where does $upload_logo get set?

Do you mean that the actual size of the file is 0 or that the size of the file as reported by $size is 0?

jaa
 
Dear jaa,

thank you for your reply. I should change $upload_log to $upfile like this

##--------
# uploadImage()
##--------
sub uploadImage
{

$upfile = lc(join '', split / /,(split/\\/, param("logo"))[-1]);

$abs_file_to_create .= "/usr/local/etc/httpd/vhosts/$abs_file_to_create .= "$upfile";

open(NEW_FILE,">$abs_file_to_create") || die "Can't open $abs_file_to_create for output: $!";
binmode NEW_FILE;
binmode $upfile;
while ($size = read($upfile, $data, 8192))
{
print NEW_FILE $data;
}
close NEW_FILE;

}

I did this and it didn't work and the old code above used to work now it doesn't. Do you see anything else I can do?

The other question you had was about the file size. when it transfer the file to the web server the file size was 0. the file that is on my desktop is example 642. it transfer the name of the file but not the file. How can I change this?
Thank you for your knowledge, wisdom and time!

There
 
I think the problem is that you are stripping the path from the file name that you are uploading. Instead try
Code:
sub uploadImage
{

    my $file_name = $q->param('logo');
    my $save_name = lc(join '', split / /,(split/\//, $file_name)[-1]);

    my $abs_file_to_create .= "temp/"; # replace with proper path
    $abs_file_to_create .= $save_name;

    open(NEW_FILE,">$abs_file_to_create") || die "Can't open $abs_file_to_create for output: $!";
    binmode NEW_FILE;
    my ($data);
    while (read($file_name, $data, 8192)) {
        print NEW_FILE $data;
    }
    close NEW_FILE;
}
jaa
 
I was using the OO interface of CGI so you will have to change the [tt]$q->param('logo')[/tt] back to
Code:
my $file_name = param('logo');
jaa
 
jaa,

now uploading a file to the add section works. this is the code

##--------
# uploadImage()
##--------
sub uploadImage
{

# need this transfer the right size of the file
$upload_logo = param('logo');
$logo = lc(join '', split / /,(split/\\/, param("logo"))[-1]);
# $upfile = lc(join '', split / /,(split/\\/, $logo)[-1]);

$abs_file_to_create .= "/usr/local/etc/httpd/vhosts/$abs_file_to_create .= "$logo";

open(NEW_FILE,">$abs_file_to_create") || die "Can't open $abs_file_to_create for output: $!";
binmode NEW_FILE;
while (read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
}
close NEW_FILE;

}


In the edit section, how do I tell it to replace the old logo with the new logo? Thank you for your knowledge, wisdom and time!

There
 
just to make sure I hope you are using

METHOD=&quot;POST&quot; ENCTYPE=&quot;multipart/form-data&quot; in the <FORM> tag where you're uploading the file.. this 0 size thing happened to me once when I accidentally missed adding the enctype in form part in a hurry.. so just make sure

cheers!
san.
 
here is a code snippet for your help.. this definately works:

The form part

Code:
<form name=&quot;main&quot; action=&quot;/cgi-bin/path/addnewurl.pl&quot; METHOD=&quot;POST&quot;  ENCTYPE=&quot;multipart/form-data&quot;>
<input type=&quot;file&quot; ACCEPT=&quot;image/gif&quot; size=&quot;20&quot; value=&quot;&quot; name=&quot;filename&quot;>
<input type=&quot;submit&quot; value=&quot;Add URL!&quot;> <input type=&quot;reset&quot;>
</form>

-----

The CGI part:

Code:
use CGI qw/:standard -no_debug/;

sub upload
{
  my $filename = $q->param('filename');
  $filename =~ /.+\.(\S{0,})$/;
	$ext = lc ($1);

	if ( ($ext ne 'jpg') and ($ext ne 'gif') )
	{
		print (&quot;$ext extension is not supported!.<br>Upload file extension must be gif/jpg!&quot;);
		exit;
	}

	my ($bytesread, $buffer, $totalbytes, $my_data);
	while ($bytesread=read($filename,$buffer,1024))
	{
		$totalbytes += $bytesread;
		$my_data .= $buffer;
	}
  return $my_data;
}

san.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top