If both passive and active modes are not working in the script, but the command line is, I would suspect a possible proxy. The client might be picking up your proxy settings, but the script isn't.
If that were the case it typically wouldn't make much sense for the put to be working, but not the ls. The proxy should deny both. Of course different proxies have different levels of granularity in their policies. Maybe it's allowing ls, but not dir or applying a "guest" policy to allow puts, but not dir or ls.