파일이 있는지 확인하려면 절대 경로를 사용해야합니다.
$abs_path = '/var/www/example.com/public_html/images/';
$file_url = 'http://www.example.com/images/' . $filename;
if (file_exists($abs_path . $filename)) {
echo "The file exists. URL:" . $file_url;
} else {
echo "The file does not exist";
}
CMS 또는 PHP 프레임 워크 용으로 작성하는 경우 내가 아는 한 모두 문서 루트 경로에 대해 상수를 정의했습니다.
예를 들어 WordPress는 사이트 URL뿐만 아니라 코드를 사용하여 서버의 파일 작업에 전역 적으로 사용할 수있는 ABSPATH를 사용합니다.
Wordpress 예 :
$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;
if (file_exists($image_path)) {
echo "The file exists. URL:" . $file_url;
} else {
echo "The file does not exist";
}
나는 여기에 여분의 마일을 갈거야 :). 이 코드는 유지 관리가 많이 필요하지 않고 상당히 견고하므로 짧은 if 문으로 작성합니다.
$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;
echo (file_exists($image_path))?'The file exists. URL:' . $file_url:'The file does not exist';
속기 IF 문은 다음과 같이 설명합니다.
$stringVariable = ($trueOrFalseComaprison > 0)?'String if true':'String if false';