getcwd() 函数返回当前工作目录(Current Working Directory)的路径。
当前工作目录是 PHP 脚本执行时默认的文件操作目录,所有相对路径都是基于此目录进行解析的。使用 getcwd() 可以了解脚本当前的工作环境。
getcwd(): string|false
getcwd() 函数不接受任何参数。
| 返回值 | 描述 |
|---|---|
| string | 成功时返回当前工作目录的绝对路径字符串 |
| FALSE | 获取失败时返回 false(通常表示权限问题或目录被删除) |
<?php
// 获取当前工作目录
$currentDir = getcwd();
echo "当前工作目录: " . $currentDir . "<br>";
// 显示目录详细信息
echo "目录类型: " . gettype($currentDir) . "<br>";
echo "目录长度: " . strlen($currentDir) . " 字符<br>";
?>
<?php
// 保存原始工作目录
$originalDir = getcwd();
echo "原始工作目录: $originalDir <br><br>";
// 改变工作目录
if (chdir('/tmp')) {
echo "成功切换到 /tmp 目录<br>";
echo "当前工作目录: " . getcwd() . "<br><br>";
// 再切换到上级目录
chdir('..');
echo "切换到上级目录后的工作目录: " . getcwd() . "<br><br>";
// 返回到原始目录
chdir($originalDir);
echo "返回原始目录: " . getcwd();
} else {
echo "目录切换失败";
}
?>
<?php
// 获取当前工作目录
$cwd = getcwd();
echo "当前工作目录: $cwd<br>";
// 使用相对路径打开文件
$relativePath = 'test.txt';
if (file_exists($relativePath)) {
echo "文件 $relativePath 存在(相对于当前目录)<br>";
echo "文件的绝对路径: " . realpath($relativePath) . "<br>";
} else {
echo "文件 $relativePath 不存在<br>";
echo "搜索的绝对路径: " . $cwd . '/' . $relativePath . "<br>";
}
// 创建相对路径的文件
file_put_contents('test.txt', '这是一个测试文件');
echo "文件已创建: " . realpath('test.txt') . "<br>";
// 清理
unlink('test.txt');
?>
<?php
/**
* 安全地获取当前工作目录
* @return string 当前工作目录或错误信息
*/
function getCurrentDirectory() {
$cwd = getcwd();
if ($cwd === false) {
// 获取失败,尝试其他方法
if (function_exists('debug_backtrace')) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
if (isset($trace[0]['file'])) {
return dirname($trace[0]['file']);
}
}
return "无法获取当前工作目录";
}
return $cwd;
}
// 测试函数
echo "当前工作目录: " . getCurrentDirectory() . "<br>";
// 模拟错误情况(在正常情况下这不会发生)
// 但可以通过chdir到一个不存在的目录然后删除它来测试
?>
<?php
// main.php 文件
echo "main.php 的工作目录: " . getcwd() . "<br>";
// 包含另一个文件
include 'includes/subdir/include_test.php';
// 在函数中使用
function testGetcwd() {
echo "函数内的工作目录: " . getcwd() . "<br>";
}
testGetcwd();
?>
<?php
// includes/subdir/include_test.php 文件
echo "include_test.php 的工作目录: " . getcwd() . "<br>";
echo "本文件的真实路径: " . __FILE__ . "<br>";
echo "本文件的目录: " . __DIR__ . "<br><br>";
?>
输出示例:
main.php 的工作目录: /var/www/html include_test.php 的工作目录: /var/www/html 本文件的真实路径: /var/www/html/includes/subdir/include_test.php 本文件的目录: /var/www/html/includes/subdir 函数内的工作目录: /var/www/html
<?php
/**
* 简单的文件浏览器
*/
function simpleFileBrowser($directory = null) {
// 如果没有指定目录,使用当前工作目录
if ($directory === null) {
$directory = getcwd();
}
// 验证目录
if (!is_dir($directory)) {
return "错误: $directory 不是有效的目录";
}
// 保存原始目录以便返回
$originalDir = getcwd();
// 切换到目标目录
if (!chdir($directory)) {
return "错误: 无法访问目录 $directory";
}
$html = "<h3>当前目录: " . getcwd() . "</h3>";
$html .= "<table class='table table-striped'>";
$html .= "<thead><tr><th>名称</th><th>类型</th><th>大小</th></tr></thead>";
$html .= "<tbody>";
// 获取目录内容
$items = scandir(getcwd());
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$fullPath = getcwd() . '/' . $item;
if (is_dir($fullPath)) {
$type = "目录";
$size = "-";
} else {
$type = "文件";
$size = filesize($fullPath) . " 字节";
}
$html .= "<tr><td>$item</td><td>$type</td><td>$size</td></tr>";
}
$html .= "</tbody></table>";
// 返回原始目录
chdir($originalDir);
return $html;
}
// 使用示例
echo simpleFileBrowser();
?>
| 特性 | __DIR__ | getcwd() |
|---|---|---|
| 定义 | 魔术常量,返回文件所在的目录 | 函数,返回当前工作目录 |
| 值的变化 | 在编译时确定,不会改变 | 运行时确定,可能改变 |
| 是否受 chdir() 影响 | 否 | 是 |
| 在包含文件中的值 | 返回包含文件的目录 | 返回脚本当前的工作目录 |
| 性能 | 常量,性能最好 | 函数调用,有轻微开销 |
| 推荐使用场景 | 需要文件自身位置时 | 需要知道脚本运行时的工作环境时 |
这是因为 Web 服务器(如 Apache、Nginx)和命令行界面(CLI)有不同的工作目录设置:
例如,在 CLI 中执行 php script.php 时,工作目录就是执行命令时所在的目录。
最佳实践:
__DIR__getcwd()realpath() 规范化路径$originalDir = getcwd();