Repository navigation
setPath
Sets a new working directory path for file management. If an absolute or relative path cannot be resolved directly, the method dynamically searches the current directory context-checking both parent paths and immediate child folders, to automatically figure out the absolute path.
📌 Successfully calling
setPath()resets all public instance metadata variables (includingfile,bytes,size,created,modified,accessed,filescount, andfolderscount) to prevent stale data from the previous path.
setPath(string $path): bool
path (string) : The directory path or partial folder name to resolve and set.
(bool)
: Returns true if a valid absolute directory path was successfully resolved and set; otherwise, returns false.
When a string is supplied to setPath, it evaluates the target directory using a three-tiered fallback strategy:
- Direct Resolution: It first tests if the input string is already a valid absolute path or relative path via PHP's
realpath(). - Upward Matching (
getParents): If direct resolution fails, it scans up the parent directory chain to see if an ancestor path matches the partial target (e.g., jumping back up to a previous folder). - Downward Matching (
getFolders): If parent matching fails, it scans the immediate child folders of the current directory to see if any child folder matches the partial target name.
- Standard Absolute Path Placement
If you provide a complete, existing absolute path, it normalizes the slashes and updates the state.
$file->setPath('/var/www/project');
// Path becomes: /var/www/project/
- Resolving via Parent History (Upward)
If you pass a partial folder name that exists higher up in the current directory's parent tree, getParents() intercepts it and resolves the full path.
$file->setPath('www');
// Path becomes: /var/www/
- Resolving via Child Folders (Downward)
If you pass a partial folder name that exists lower down in the current directory's child tree, getFolders() intercepts it and resolves the full path.
$file->setPath('src');
// Path becomes: /var/www/project/src/
- Triggers a warning if the path is invalid or does not exist on disk.
Home | CoreyFile | Environment