1

Say I want to find a file in one of the parent directories of the script. Consider this file tree:

Folder0
    Folder1
        needle.php
        Folder2
            Folder3
                script.php

I'm running script.php and I need to find which directory contains needle.php. I do this:

while (!file_exists($nameFile)) {
    chdir('..');
    }
return getcwd();

This returns "Folder0\Folder1\";

What is the proper way to transform this path into a URL of this file? (e.g. `http://hostname.com/Folder0/Folder1/needle.php")

Or is there a better way to get a URL of a file on a server?

Stas Bichenko
  • 13,013
  • 8
  • 45
  • 83

4 Answers4

6
getcwd()

give You current dir from server root like /home/vincent/public_html/folder You Can get DOCUMENT_ROOT By

  $_SERVER['DOCUMENT_ROOT'];//  /home/vincent/public_html

You Must Remove DOCUMENT_ROOT from first of getcwd and concat with Host Name. like this

  echo str_replace("\\",'/',"http://".$_SERVER['HTTP_HOST'].substr(getcwd(),strlen($_SERVER['DOCUMENT_ROOT'])));

output:

 http://www.domain.com/folder
mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
0

You can use The RecursiveDirectoryIterator class

and here is a good tutorial that might help you

I hope this help, if anymore help needed we are here and glad to help :)

Seder
  • 2,735
  • 2
  • 22
  • 43
0
while (!file_exists($nameFile)) {
    chdir('..');
}
return "http://".$_SERVER['HTTP_HOST'].str_replace('\\', '/', getcwd());
0

$basePath="http://hostname.com/"; $fileName="needle.php"; $folderPath="Folder0\Folder1\";

$path=str_replace("\","/",$folderPath); $pathToFile=$basePath.$path.$fileName;

echo $pathToFile;

Make a function out of it !

Lucian

Lucian Depold
  • 1,999
  • 2
  • 14
  • 25