LoginSignup
4
4

More than 5 years have passed since last update.

PHP 良く使いそうな関数メモ

Last updated at Posted at 2015-03-16

ファイル存在確認

common
// 存在しない場合は生成
function checkDir($path, $mode = 0777) {
    if (!file_exists($path)) {
        // 再帰的にディレクトリを生成
        if (!@mkdir($path, $mode, true)) return false;
    }
    return true;
}

コンテンツ取得

common
function fetchContents($url, $datas = array(), $method = 'POST') {
    $datas = http_build_query($datas, '', '&');

    $header = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($datas)
        );

    $context = array(
        'http' => array(
        'method'  => $method,
        'header'  => implode("\r\n", $header),
        'content' => $datas
        )
    );

    return file_get_contents($url, false, stream_context_create($context));
}

ランダム文字列生成

common
function makeRandStr($length = 8) {
    $str = array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9'));
    $randStr = '';
    for ($i = 0; $i < $length; $i++) {
        $randStr .= $str[rand(0, count($str))];
    }
    return $randStr;
}

範囲内の値か確認

common
    function checkRangeValue($val, $max, $min = 0) {
        return ($val <= $max && $val >= $min) ? true : false;
    }
4
4
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
4
4