Gallium oxide (Ga2O3), a wide bandgap semiconductor, has been extensively studied for its potential applications in deep ultraviolet photodetectors and next-generation power electronic devices. To enhance the optoelectronic properties of Ga2O3 films, a controllable doping strategy is proposed in this work. Using a sol-gel method, Ga2O3 films doped with magnesium (Mg) are prepared, with the Mg concentration adjustable through the precursor solution. The results indicate that the bandgap of Ga2O3 increases with higher doping levels, while the electrical conductivity decreases proportionally. To evaluate their optoelectronic characteristics, a series of photodetectors with Mg-doped Ga2O3 active layers are fabricated. Under a 254 nm incident light, the device with an optimal doping concentration of 4.2% demonstrates the best performance, achieving the highest responsivity (R) of 1.97 A/W and a photo-dark current ratio of 2.6 × 103. Furthermore, density functional theory calculations are employed to provide a detailed analysis of the fundamental mechanisms behind the enhanced optoelectronic properties. This approach to controllable and optimized doping in Ga2O3 films shows promise for future applications in semiconductor devices.