There is a file in which we know the path to the root of the server;

/var/www/papka/index.php 

There are files that only know the relative paths to the file (above).

  ../../path/files/1.jpg img/files/1.jpg ../new/2.png 

The essence of the question is what - you need to know the full path from the index.php file, find out the full paths to the server root, for files in which we know only relative paths, and the location of the files may vary.

That is, the output should be something like this:

 /var/www/over/list/path/files/1.jpg /var/www/papka/img/files/1.jpg /var/www/list/new/2.png 

To anyone who advises a beautiful and versatile solution, I will appreciate the crane!

1 answer 1

 <?php $path_parts = pathinfo('/var/www/papka/index.php'); $dir = $path_parts['dirname'] + '/'; var_dump(realpath($dir + '../../path/files/1.jpg')); var_dump(realpath($dir + 'img/files/1.jpg')); var_dump(realpath($dir + '../new/2.png')); 
  • realpath () doesn't fit :) - PinkTux
  • 2
    @PinkTux So this is a school lab and you have to do it yourself :). - E_p
  • No brothers does not fit, realpath () works differently on Windows and Linux servers. On Linux, everything is a bundle, but on Land-Lots machines, a backslash and drive letter go in the appendix. I had to reinvent the wheel with a cycle and a breakdown into arrays, but it does not work ugly correctly. And this is not a laboratory work, it has long been out of school age. - PegasBY
  • @PegasBY I will please you with the wrong decision. 1. If you need Nada cross-platform, then for Windows you should keep it as it returns. 2. If this is a web application then having 2 servers on different Win / Linux platforms is even more stupid. 3. Who prevents you from fooling yourself with the masses and just get rid of the str_replace. - E_p
  • I pondered and tried your version, but it works incorrectly (tested), and on the question of why on different platforms, it doesn’t depend on me, but it’s up to me to guarantee the result under any conditions. - PegasBY