Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

ファイル存在確認

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;
    }
terumichi1209
WEBエンジニアです!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away