On Mar 30, 2017 8:25 PM, "Rasmus Schultz" <ras...@mindplay.dk> wrote:
Today, I ran into a very hard-to-debug problem, in which paths (to SQL files, in a database migration script) were kept in a map, persisted to a JSON file, and this file was moved from a Windows to a Linux file-system - because the paths on the Linux system had forward slashes, the files appeared to be missing from the map. Related questions are very commonly asked by Windows users, indicating that this is a common problem: http://stackoverflow.com/questions/14743548/php-on- windows-path-comes-up-with-backward-slash http://stackoverflow.com/questions/5642785/php-a-good- way-to-universalize-paths-across-oss-slash-directions http://stackoverflow.com/questions/6510468/is-there-a- way-to-force-php-on-windows-to-provide-paths-with-forward-slashes The answers that are usually given (use DIRECTORY_SEPARATOR, use str_replace() etc.) is that by default you automatically get cross-platform inconsistencies, and the workarounds end up complicating code everywhere, and sometimes lead to other (sometimes worse) portability problems. The problem is worsened by functions like glob() and the SPL directory/file traversal objects also producing inconsistent results. Returning backslashes on Windows seems rather unnecessary in the first place, since forward slashes work just fine? Might I suggest changing this behavior, such that file-system paths are consistently returned with a forward slash? Though this is more likely to fix rather than create issues, this could be a breaking change in some cases, so there should probably be an INI setting that enables the old behavior. Thoughts? It is true (works) only on Windows because PHP does the conversion transparently for you. It will miserably fails if your json string are processed as paths with other tools or languages not doing this magic for you. Cheers Pierre