Upgrade to Pro — share decks privately, control downloads, hide ads and more …

可能な限り確実にmkdirを成功させるには / Make mkdir

可能な限り確実にmkdirを成功させるには / Make mkdir

PHPerKaigi 2023 のLTの資料です

Yuichi Sugiyama

March 25, 2023
Tweet

More Decks by Yuichi Sugiyama

Other Decks in Technology

Transcript

  1. Whoami • ਿࢁ ༞Ұ @oogFranz • αΠϘ΢ζͰେاۀ޲͚άϧʔϓ΢ΣΞ 
 Garoon (Ψϧʔϯ)

    ࡞ͬͯΔ • ෳۀϛϡʔδγϟϯ • MASHݭָஂɺFried Banana CabinɺϒϧʔάϥεˑϙϦε 
  2. σΟϨΫτϦύεΛࢦఆ͢ΔͱWarning makeDirectory('test/foo/bar'); //Warning: mkdir(): No such file or directory in

    /usr/ local/code/mkdir.php on line 9 • testσΟϨΫτϦɺfoo σΟϨΫτϦ͕ଘࡏ͠ͳ͍৔߹ 
 Warning͕ൃੜ͢Δ • σΟϨΫτϦ͸࡞੒͞Εͳ͍
  3. ϚχϡΞϧΛͪΌΜͱಡΉ • recursiveΦϓγϣϯ͸mkdir ͷ —parents, -p Φϓγϣϯͱಉ͡ • ਌σΟϨΫτϦΛඞཁʹԠͯ͡࡞੒ͯ͘͠ΕΔ mkdir(

    string $directory, int $permissions = 0777, bool $recursive = false, ?resource $context = null ): bool
  4. is_dir, fi le_existsΛ࢖͏ function makeDirectory(string $path): bool { if (is_dir($path))

    { return true; } if (file_exists($path)) { return false; } return mkdir($path, recursive: true); } ✴ fi le_exists() ͕ trueͷͱ͖͸݁ՌతʹσΟϨΫτϦ͕࡞੒Ͱ͖ͳ͍ͷͰ஫ҙ
  5. clearstatcache()Λ࢖͏ function makeDirectory(string $path): bool { clearstatcache(); if (is_dir($path)) {

    return true; } if (file_exists($path)) { return false; } return mkdir($path, recursive: true); }
  6. clearstatcache()Λ࢖͏ function makeDirectory(string $path): bool { clearstatcache(); if (is_dir($path)) {

    return true; } if (file_exists($path)) { return false; } return mkdir($path, recursive: true); } JT@EJS͕ਖ਼͘͠ಈ࡞͢Δ
  7. Τϥʔͷ཈͑ํ • Τϥʔ੍ޚԋࢉࢠ `@` Λ࢖͏ 
 खܰɻͳΜ͔ෛ͚ͨؾ͕͢Δɻ • error_reporting Ͱ

    E_WARNINGΛ཈͑Δ 
 ଞͷE_WARNING΋཈͑ͯ͠·͏ͷͰ͓קΊ͠ͳ͍ • set_error_handler ͰWarningΛΩϟον͢Δ 
 ਅ໘໨ʹ΍ΔͳΒ͜Εɻ • ഉଞ੍ޚʁʁ 
 ΦʔόʔΩϧͰ͸ʁ͜ͷϝιουҎ֎͔Βͷݺͼग़͠ʹ͸উͯͳ͍ɺɺɺ
  8. Τϥʔ੍ޚԋࢉࢠΛ࢖ͬͨ৔߹ function makeDirectory(string $path): bool { if (@mkdir($path, recursive: true)

    === false) { clearstatcache(); return is_dir($path); } return true; } • Τϥʔ੍ޚʹ೚ͤͯࣄલͷσΟϨΫτϦνΣοΫ͸ෆཁʢ͔΋ʣ