public static createDirectory ( string $path, integer $mode = 509, boolean $recursive = true ) : boolean | ||
$path | string | path of the directory to be created. |
$mode | integer | the permission to be set for the created directory. |
$recursive | boolean | whether to create parent directories if they do not exist. |
return | boolean | whether the directory is created successfully |
public static function createDirectory($path, $mode = 0775, $recursive = true)
{
if (is_dir($path)) {
return true;
}
$parentDir = dirname($path);
// recurse if parent dir does not exist and we are not at the root of the file system.
if ($recursive && !is_dir($parentDir) && $parentDir !== $path) {
static::createDirectory($parentDir, $mode, true);
}
try {
if (!mkdir($path, $mode)) {
return false;
}
} catch (\Exception $e) {
if (!is_dir($path)) {
// https://github.com/yiisoft/yii2/issues/9288
throw new \yii\base\Exception("Failed to create directory \"{$path}\": " . $e->getMessage(), $e->getCode(), $e);
}
}
try {
return chmod($path, $mode);
} catch (\Exception $e) {
throw new \yii\base\Exception("Failed to change permissions for directory \"{$path}\": " . $e->getMessage(), $e->getCode(), $e);
}
}
public function attachFile() { try { $model = $this->owner; $fileName = Inflector::slug($model->fullAddress) . '.pdf'; $folder = $this->getModelSubDir() . '/'; $model->pdf_path = $fileName; $pdf = new Pdf(['mode' => Pdf::MODE_UTF8, 'destination' => Pdf::DEST_FILE, 'content' => Yii::$app->view->render('@frontend/views/site/real-estate/print', ['model' => $model]), 'methods' => ['SetHeader' => ['Rapport ' . $model->fullAddress], 'SetFooter' => ['|Pagina {PAGENO}|']], 'filename' => Yii::getAlias('@uploadsBasePath') . "/files/{$folder}{$fileName}"]); $this->file = $pdf; BaseFileHelper::removeDirectory(Yii::getAlias('@uploadsBasePath') . "/files/{$folder}"); if (!BaseFileHelper::createDirectory(Yii::getAlias('@uploadsBasePath') . "/files/{$folder}")) { throw new Exception(Yii::t('app', 'Failed to create the file upload directory')); } // Save and update model $pdf->render(); $model->save(); return true; } catch (yii\base\Exception $e) { return $e->getMessage(); } }