/*****************************************
* Put somewhere this code, then call it
*****************************************/
/**
* Create a temp file and get full path
* @param string $prefix (optional) Name prefix
* @return string Full temp file path
* @throws \yii\web\NotFoundHttpException When tmp directory doesn't exist or failed to create
*/
function getTempFile ( $prefix = 'temp' ) {
$tmpDir = \Yii::$app->runtimePath.'/tmp';
if ( !is_dir($tmpDir) && (!@mkdir($tmpDir) && !is_dir($tmpDir)) ) {
throw new \yii\web\NotFoundHttpException ('temp directory does not exist');
}
return tempnam( $tmpDir, $prefix );
}
/*******************
* EXAMPLE OF USAGE
*******************/
// absolute temp file path
$tmpFile = getTempFile();
/** @var string $contents */
$contents = <<<CONTENTS
Lorem ipsum dolor sit amet, tritani abhorreant in sea, wisi vitae disputationi ad eam, tacimates molestiae adolescens sed at.
CONTENTS;
/** @var string $file */
file_put_contents( $tmpFile, $contents );
// remove the file
@unlink($tmpFile);
Чтобы увидеть комментарии, нужно быть участником сообщества
Регистрация