戻る

フルパスからディレクトリ名を取り出す(参考)

dirname()関数

dirname()関数を使うと、引数に指定したファイルのフルパス情報から「ファイル名+拡張子」部分を除いたディレクトリ名だけを取り出せます。この例のように、ドライブ名からのフルパスを引数に指定した場合は「ドライブ名+ディレクトリ名」が返され、相対パスのようにディレクトリ名から始まるパスを指定した場合は「ディレクトリ名」だけが返されます。返り値として取得された文字列を処理で使う際は、末尾には「\」や「/」はついていないことに注意してください。

PHP

<?php
header("Content-type: text/html; charset=utf-8");

$fullpath = "C:\xampp\htdocs\images\gihyo.gif";

echo $fullpath . "のディレクトリは<br>";
echo dirname($fullpath) . "<br><br>";

$fullpath = "images/gihyo.gif";
echo $fullpath . "のディレクトリは<br>";
echo dirname($fullpath) . "<br><br>";

$fullpath = __FILE__;
echo $fullpath . "のディレクトリは<br>";
echo dirname($fullpath) . "<br><br>";
?>

inserted by FC2 system