LoginSignup
13
10

More than 5 years have passed since last update.

[CakePHP 2.x] phpで拡張子取得方法、ファイルを使う際の便利関数 pathinfo

Posted at

cakephp3日目。
拡張子取得したついでに、pathinfoの使い方を学びましたよ。
cakeというかphpですね。。

$ext = pathinfo($file, PATHINFO_EXTENSION);

もしくは

$pathinfo = pathinfo($file);
$ext = $pathinfo['extention']

な感じです。

pathinfoでは他にもこんな使い道も。

$file = "/hoge/fuga/index.php";
---
// 親ディレクトリのパス ⇒ ”/hoge/fuga”
$dirName = pathinfo($file, PATHINFO_DIRNAME);
// ファイル名 ⇒ ”index.php”
$baseName = pathinfo($file, PATHINFO_BASENAME);
// 拡張子無しファイル名 ⇒ "index"
$fileName = pathinfo($file, PATHINFO_FILENAME);
// 拡張子 ⇒ ”php”
$extension = pathinfo($file, PATHINFO_EXTENSION);

参考

13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10