0

Say I have a badly formatted path /public/var/www/html/images\uploads\

Are there any performance benefits between these two methods to "normalize" the slashes, or is it just a different way of doing things?

  1. realpath($path) . DIRECTORY_SEPARATOR
  2. str_replace('\\', '/', $path);
Alex
  • 9,215
  • 8
  • 39
  • 82
  • Just glanced M. Cummings file path normalizer. Composer: `phpclasses/file-path-normalizer`. If you want to avoid realpath, but want something more thorough than str_replace. – Progrock Aug 25 '17 at 03:17

1 Answers1

0

realpath() might and probably does take a tad more computation but it is doing more than str_replace() would. As to which you would use is up to you and depends on the application. realpath() will not only fix the format of strings.. but will also verify that a file exists by that name. Also, using realpath() will, in most cases, make your code more readable and easier to understand because it's naming better corresponds to it's functionality here (depending, again, on the application).

realpath()

Phillip Weber
  • 554
  • 3
  • 9
  • As realpath can expand symlinks, it must be crawling the file system, whereas str_replace never looks at it. – Progrock Aug 25 '17 at 03:09