Windows는 대소문자를 구분하지 않지만 Linux는 구분하기 때문에 배포 시 파일을 찾을 수 없는 오류가 발생합니다. 이런 크로스 플랫폼 문제를 해결하는 방법들을 알아보겠습니다.
<?php
function safePath($path) {
// 경로 구분자 통일
$path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
// 실제 파일 시스템에서 대소문자 확인
if (file_exists($path)) {
return realpath($path);
}
return false;
}
// 사용 예
$configPath = safePath('config/Database.php');
if ($configPath) {
require_once $configPath;
}
<?php
function findFileIgnoreCase($directory, $filename) {
$files = scandir($directory);
foreach ($files as $file) {
if (strtolower($file) === strtolower($filename)) {
return $directory . DIRECTORY_SEPARATOR . $file;
}
}
return false;
}
// 사용 예
$actualFile = findFileIgnoreCase('./models', 'UserModel.php');
if ($actualFile) {
include $actualFile;
}
<?php
function findFileRecursive($baseDir, $targetFile) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($baseDir)
);
foreach ($iterator as $file) {
if (strtolower($file->getFilename()) === strtolower($targetFile)) {
return $file->getPathname();
}
}
return false;
}
// 사용 예
$found = findFileRecursive('./src', 'myclass.php');
<?php
spl_autoload_register(function ($className) {
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
$directories = ['src/', 'lib/', 'classes/'];
foreach ($directories as $dir) {
$fullPath = $dir . $classPath;
// 정확한 경로 먼저 시도
if (file_exists($fullPath)) {
require_once $fullPath;
return;
}
// 대소문자 무관 검색
$found = findFileIgnoreCase(dirname($dir . $classPath), basename($classPath));
if ($found) {
require_once $found;
return;
}
}
});
<?php
class ConfigManager {
private $configPaths = [
'config/app.php',
'Config/App.php',
'CONFIG/APP.PHP'
];
public function loadConfig($configName) {
foreach ($this->configPaths as $path) {
$fullPath = str_replace('app', $configName, $path);
if (file_exists($fullPath)) {
return include $fullPath;
}
}
throw new Exception("설정 파일을 찾을 수 없습니다: $configName");
}
}
<?php
class TemplateLoader {
private $templateDirs = ['templates/', 'views/', 'Templates/'];
public function load($templateName) {
foreach ($this->templateDirs as $dir) {
// 여러 확장자 시도
$extensions = ['.php', '.tpl', '.html'];
foreach ($extensions as $ext) {
$path = $dir . $templateName . $ext;
if (file_exists($path)) {
return $path;
}
// 대소문자 변형 시도
$variations = [
strtolower($templateName),
strtoupper($templateName),
ucfirst($templateName)
];
foreach ($variations as $variation) {
$varPath = $dir . $variation . $ext;
if (file_exists($varPath)) {
return $varPath;
}
}
}
}
return false;
}
}
<?php
function getImagePath($imageName, $baseDir = 'images/') {
$extensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
foreach ($extensions as $ext) {
$variations = [
$imageName . $ext,
strtolower($imageName) . $ext,
strtoupper($imageName) . $ext
];
foreach ($variations as $variation) {
$fullPath = $baseDir . $variation;
if (file_exists($fullPath)) {
return $fullPath;
}
}
}
return $baseDir . 'default.png'; // 기본 이미지
}
<?php
function validatePaths($pathsToCheck) {
$errors = [];
foreach ($pathsToCheck as $path) {
if (!file_exists($path)) {
// 대소문자 문제인지 확인
$dir = dirname($path);
$filename = basename($path);
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file) {
if (strtolower($file) === strtolower($filename) && $file !== $filename) {
$errors[] = "대소문자 불일치: $path (실제: $dir/$file)";
break;
}
}
} else {
$errors[] = "파일 없음: $path";
}
}
}
return $errors;
}
// 사용 예
$paths = ['Config/Database.php', 'Models/User.php', 'Views/home.php'];
$errors = validatePaths($paths);
if (!empty($errors)) {
foreach ($errors as $error) {
echo $error . "\n";
}
}
이러한 방법들을 사용하면 Windows와 Linux 간의 대소문자 경로 문제를 효과적으로 해결할 수 있습니다. 특히 개발 단계에서 검증 도구를 사용하면 배포 전에 문제를 미리 발견할 수 있습니다.