Why does the fungus attack my feet?

Fungi. Fungus like dark, damp, warm places. Your feet, especially when wearing shoes fit this criteria very well. Try using an anti-fungal foot powder, or any foot powder that will absorb moisture, like amens medicated powder. If your toenails are affected, you may need to seek professional assistance to cure the problem. There are several oral anti-fungal medicines available, but treatment is long term.
Control moisture. Foot fungus likes dark, warm, moist areas to live. (sounds like the inside of your shoe, right?) keep your feet washed well and dry completely. Use powders as needed. Use powder in your shoes or a fine mist of lysol to kill fungus in your shoes. Otc antifungal creams work well for active cases of fungus.