在生成或保存文件時我們經常會遇到文件夾不存在時報錯的情況,使用以下方法即可解決
//判斷文件夾是否存在,沒有則新建 function path_exists($path){ if (!function_exists($path)) { mkdirs($path); } } //創建文件夾 function mkdirs($dir, $mode = 0777) { if (is_dir($dir) || @mkdir($dir, $mode)) { return true; } if (!mkdirs(dirname($dir), $mode)) { return false; } return @mkdir($dir, $mode); }
使用方法:
path_exists("Upload/wechat/images/");