just one mistake $imgname = $file; replace it with $imgname = substr($file ,0 ,-4);
if anyone still need another solution , you might try this.i made it to when i transferred my wordpress to joomla k2 "troublesome".1. make php file at the top of your joomla directory. "anything.php"2.copy and paste the code in it then...
More