PlayHardGoPro
PlayHardGoPro

Reputation: 2942

Get full path while opening a directory

I'm creating a script to run through a given folder and list all files inside it.
The problem is: The user may or may not give the full path or just the folder name (if the target folder is inside the script folder).

Is there a way to get the full/absolute path of that folder even if the user gives only its name ?

    //Check if the folder path was given as an argument    
    if( $argc >= 2) {
       $folderPath = $argv[1]; //Read the folder path argument 
        if( !is_dir($folderPath) ) {
          echo "Folder does NOT exists !";
       }
       else {      
          if( $handle = opendir($folderPath) ) {
             //Find the $folderPath absolute path  here 

$folderPath may be:
- C:\wamp64\www\myfolder\documents
- Or just: documents

either cases the script will find the folder, open it and list it's files. But I need to write the fullPath later on the code.

Upvotes: 2

Views: 55

Answers (1)

wordragon
wordragon

Reputation: 1357

You may want realpath, which cuts through all the issues - relative paths, symbolic links, etc.

Upvotes: 1

Related Questions