Measurements of the branching fraction ratio B(ϕ→μ+μ−)/B(ϕ→e+e−) with Ds+→π+ϕ and D+→π+ϕ decays, denoted Rϕπs and Rϕπd, are presented. The analysis is performed using a dataset corresponding to an integrated luminosity of 5.4fb−1 of pp collision data collected with the LHCb experiment. The branching fractions are normalised with respect to the B+→K+J/ψ(→e+e−) and B+→K+J/ψ(→μ+μ−) decay modes. The combination of the results yields Rϕπ=1.022±0.012(stat)±0.048(syst). The result is compatible with previous measurements of the ϕ→ℓ+ℓ− branching fractions and predictions based on the Standard Model.