$max_size = '100000'; #set your max size value $totalbytes=0; open (IMAGESAVE,">$image_upload_directory/$final_name"); while ($bytesread=read ($file,$data,1024)) { $totalbytes+=$bytesread; if ($totalbytes > $max_size) { unlink ("$image_upload_directory/$file_name"); &FileTooLarge; #call your error sub exit; } print IMAGESAVE $data; } close IMAGESAVE;