本文整理汇总了PHP中yii\helpers\BaseFileHelper::createDirectory方法的典型用法代码示例。如果您正苦于以下问题:PHP BaseFileHelper::createDirectory方法的具体用法?PHP BaseFileHelper::createDirectory怎么用?PHP BaseFileHelper::createDirectory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\helpers\BaseFileHelper
的用法示例。
在下文中一共展示了BaseFileHelper::createDirectory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: attachFile
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();
}
}
示例2: createCachedFile
/**
* @param $srcImagePath
* @param bool $preset
* @return string Path to cached file
* @throws \Exception
*/
private function createCachedFile($srcImagePath, $pathToSave, $size = null)
{
BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true);
$size = $size ? $this->parseSize($size) : false;
// if($this->graphicsLibrary == 'Imagick'){
$image = new \Imagick($srcImagePath);
$image->setImageCompressionQuality(100);
if ($size) {
if ($size['height'] && $size['width']) {
$image->cropThumbnailImage($size['width'], $size['height']);
} elseif ($size['height']) {
$image->thumbnailImage(0, $size['height']);
} elseif ($size['width']) {
$image->thumbnailImage($size['width'], 0);
} else {
throw new \Exception('Error at $this->parseSize($sizeString)');
}
}
$image->writeImage($pathToSave);
// }
if (!is_file($pathToSave)) {
throw new \Exception('Error while creating cached file');
}
return $image;
}
示例3: upload
public function upload($upload_dir = 'product_image')
{
$file_name = [];
if ($this->validate()) {
$app_root = Yii::getAlias('@approot');
$upload_path = "{$app_root}/uploads/{$upload_dir}/";
if (!file_exists($upload_path)) {
BaseFileHelper::createDirectory($upload_path, 0777, TRUE);
} else {
if (!is_dir($upload_path)) {
unlink($upload_path);
$this->upload($upload_dir);
}
}
foreach ($this->imageFiles as $file) {
$file_path = "{$upload_path}{$file->baseName}.{$file->extension}";
$file->saveAs($file_path);
$file_info['name'] = 'product_image';
$file_info['type'] = $file->type;
$file_info['value'] = $file->baseName . '.' . $file->extension;
$file_name[] = $file_info;
}
return $file_name;
} else {
return false;
}
}
示例4: cacheDir
private function cacheDir()
{
$dir = Yii::getAlias(self::$cache_dir);
if (!file_exists($dir)) {
BaseFileHelper::createDirectory($dir, '0755');
}
return $dir;
}
示例5: attachImage
/**
*
* Method copies image file to module store and creates db record.
*
* @param $absolutePath
* @param bool $isFirst
* @return bool|Image
* @throws \Exception
*/
public function attachImage($absolutePath, $isMain = false, $name = '')
{
// var_dump($_FILES);
// die;
if (isset($_FILES['UploadForm'])) {
$fileRealName = $_FILES['UploadForm']['name']['file'];
} else {
if (isset($_FILES['gallery'])) {
$fileRealName = $_FILES['gallery']['name'];
} else {
$fileRealName = $absolutePath;
}
}
if (!preg_match('#http#', $absolutePath)) {
if (!file_exists($absolutePath)) {
throw new \Exception('File not exist! :' . $absolutePath);
}
} else {
//nothing
}
if (!$this->owner->primaryKey) {
throw new \Exception('Owner must have primaryKey when you attach image!');
}
$pictureFileName = substr(md5(microtime(true) . $absolutePath), 4, 6) . '.' . pathinfo($fileRealName, PATHINFO_EXTENSION);
$pictureSubDir = $this->getModule()->getModelSubDir($this->owner);
$storePath = $this->getModule()->getStorePath($this->owner);
$newAbsolutePath = $storePath . DIRECTORY_SEPARATOR . $pictureSubDir . DIRECTORY_SEPARATOR . $pictureFileName;
BaseFileHelper::createDirectory($storePath . DIRECTORY_SEPARATOR . $pictureSubDir, 0775, true);
copy($absolutePath, $newAbsolutePath);
if (!file_exists($newAbsolutePath)) {
throw new \Exception('Cant copy file! ' . $absolutePath . ' to ' . $newAbsolutePath);
}
if ($this->getModule()->className === null) {
$image = new models\Image();
} else {
$class = $this->getModule()->className;
$image = new $class();
}
$image->itemId = $this->owner->primaryKey;
$image->filePath = $pictureSubDir . '/' . $pictureFileName;
$image->modelName = $this->getModule()->getShortClass($this->owner);
$image->name = $name;
$image->urlAlias = $this->getAlias($image);
if (!$image->save()) {
return false;
}
if (count($image->getErrors()) > 0) {
$ar = array_shift($image->getErrors());
unlink($newAbsolutePath);
throw new \Exception(array_shift($ar));
}
$img = $this->owner->getImage();
//If main image not exists
if (is_object($img) && get_class($img) == 'rico\\yii2images\\models\\PlaceHolder' or $img == null or $isMain) {
$this->setMainImage($image);
}
return $image;
}
示例6: generateModul
public function generateModul()
{
BaseFileHelper::createDirectory($this->moduleDirectory, 509, true);
$content = $this->modulInner();
$moduleFullName = $this->moduleDirectory . $this->moduleName . '.php';
$myFail = fopen($moduleFullName, "w");
fwrite($myFail, $content);
fclose($myFail);
return true;
}
示例7: actionUpdate
public function actionUpdate($id = null)
{
$model = new Article();
if ($model->load(Yii::$app->request->post())) {
$request = Yii::$app->request->post('Article');
$id = $request['id'];
if ($id) {
$model = Article::findOne($id);
$model->attributes = $request;
}
if ($model->save()) {
$this->updateOrder('cid=' . $model->cid, '&langs=' . $model->langs);
$files = \yii\web\UploadedFile::getInstances($model, 'upload_files');
if (isset($files) && count($files) > 0) {
$mPath = \Yii::getAlias('@webroot') . '/images/article/news_' . $model->id;
if (!is_dir($mPath)) {
\yii\helpers\BaseFileHelper::createDirectory($mPath);
}
foreach ($files as $file) {
if ($request['cid'] == '12') {
$mPic = $file->baseName . '.' . $file->extension;
} else {
$mPic = 'nkc_' . substr(number_format(time() * rand(), 0, '', ''), 0, 14) . '.' . $file->extension;
}
//Upload Images
if ($file->saveAs($mPath . '/' . $mPic)) {
$image = \Yii::$app->image->load($mPath . '/' . $mPic);
$image->resize(1024, 1024);
$image->save($mPath . '/' . $mPic);
//resize images thumb
$image = \Yii::$app->image->load($mPath . '/' . $mPic);
$image->resize(250, 250);
$mThumb = $mPath . '/thumb/';
if (!is_dir($mThumb)) {
\yii\helpers\BaseFileHelper::createDirectory($mThumb);
}
$image->save($mThumb . $mPic);
}
}
}
return $this->redirect(array('index'));
} else {
print_r($model->getErrors());
exit;
}
}
if ($id) {
$model = Article::findOne($id);
} else {
$sess = Yii::$app->session->get('sessArticle');
$model->langs = $sess['langs'];
$model->cid = $sess['cid'];
}
return $this->render('update', ['model' => $model]);
}
示例8: attachImage
/**
* Copies the image to the assets folder and save's it in the database.
*
* @param string $absolutePath The path were the image is uploaded
* @param bool $isMain A flag to determine if the image is the main image
* @param string $identifier The index that has to be set for the image in the database
* @return bool|Image
* @throws \Exception
*/
public function attachImage($absolutePath, $isMain = false, $identifier = '')
{
if (!preg_match('#http#', $absolutePath)) {
if (!file_exists($absolutePath)) {
throw new \Exception('File not exist! :' . $absolutePath);
}
} else {
//nothing
}
if (!$this->owner->primaryKey) {
throw new \Exception('Owner must have primaryKey when you attach image!');
}
$pictureFileName = basename($absolutePath);
$pictureSubDir = $this->getModule()->getModelSubDir($this->owner);
$storePath = $this->getModule()->getStorePath($this->owner);
$newAbsolutePath = $storePath . DIRECTORY_SEPARATOR . $pictureSubDir . DIRECTORY_SEPARATOR . $pictureFileName;
BaseFileHelper::createDirectory($storePath . DIRECTORY_SEPARATOR . $pictureSubDir, 0775, true);
copy($absolutePath, $newAbsolutePath);
unlink($absolutePath);
if (!file_exists($newAbsolutePath)) {
throw new \Exception('Cant copy file! ' . $absolutePath . ' to ' . $newAbsolutePath);
}
$image = new Image();
$image->itemId = $this->owner->primaryKey;
$image->filePath = $pictureSubDir . '/' . $pictureFileName;
$image->modelName = $this->getModule()->getShortClass($this->owner);
$image->urlAlias = $this->getAlias($image);
$image->identifier = $identifier;
$image->name = substr(yii\helpers\Inflector::slug($pictureFileName), 0, -3);
// Get the highest position
// @todo Create function
$owner = $this->owner;
$query = (new yii\db\Query())->select('MAX(`position`)')->from(Image::tableName())->where(['modelName' => yii\helpers\StringHelper::basename($owner::className())]);
$command = $query->createCommand();
$image->position = $command->queryOne(\PDO::FETCH_COLUMN) + 1;
if (!$image->save()) {
return false;
}
// Add the translations
foreach (Yii::$app->params['languages'] as $language => $data) {
$imageLang = new ImageLang();
$imageLang->language = $language;
$image->link('translations', $imageLang);
}
if (count($image->getErrors()) > 0) {
$ar = array_shift($image->getErrors());
unlink($newAbsolutePath);
throw new \Exception(array_shift($ar));
}
$img = $this->owner->getImage();
$this->setMainImage($image);
return $image;
}
示例9: attachImage
/**
*
* Method copies image file to module store and creates db record.
*
* @param string|UploadedFile $newImage
* @param bool $isMain
* @return bool|Image
* @throws \Exception
*/
public function attachImage($newImage, $isMain = false)
{
if (!$this->owner->{$this->idAttribute}) {
throw new \Exception($this->owner->classname() . ' must have an id when you attach image!');
}
$pictureFileName = '';
if ($newImage instanceof UploadedFile) {
$sourcePath = $newImage->tempName;
$imageExt = $newImage->extension;
} else {
if (!preg_match('#http#', $newImage)) {
if (!file_exists($newImage)) {
throw new \Exception('File not exist! :' . $newImage);
}
} else {
//nothing
}
$sourcePath = $newImage;
$imageExt = pathinfo($newImage, PATHINFO_EXTENSION);
}
$pictureFileName = substr(sha1(microtime(true) . $sourcePath), 4, 12);
$pictureFileName .= '.' . $imageExt;
if (!file_exists($sourcePath)) {
throw new \Exception('Source file doesnt exist! ' . $sourcePath . ' to ' . $newAbsolutePath);
}
$pictureSubDir = $this->getModule()->getModelSubDir($this->owner);
$storePath = $this->getModule()->getStorePath($this->owner);
$destPath = $storePath . DIRECTORY_SEPARATOR . $pictureSubDir . DIRECTORY_SEPARATOR . $pictureFileName;
BaseFileHelper::createDirectory($storePath . DIRECTORY_SEPARATOR . $pictureSubDir, 0775, true);
if (!copy($sourcePath, $destPath)) {
throw new \Exception('Failed to copy file from ' . $sourcePath . ' to ' . $destPath);
}
$image = new $this->modelClass();
$image->item_id = $this->owner->{$this->idAttribute};
$image->file_path = $pictureSubDir . '/' . $pictureFileName;
$image->model_name = $this->getModule()->getShortClass($this->owner);
$image->url_alias = $this->getAlias($image);
if (!$image->save()) {
return false;
}
if (count($image->getErrors()) > 0) {
$ar = array_shift($image->getErrors());
unlink($newAbsolutePath);
throw new \Exception(array_shift($ar));
}
$img = $this->owner->getImage();
// If main image not exists
if (is_object($img) && get_class($img) == 'circulon\\images\\models\\Placeholder' or $img == null or $isMain) {
$this->setMainImage($image);
}
return $image;
}
示例10: upload
public function upload()
{
if ($this->validate()) {
$image_dir = \Yii::getAlias('@webroot');
$this->_uploaded_url = implode(DIRECTORY_SEPARATOR, ['', self::DIR_UPLOADS, self::DIR_IMAGES, '']);
foreach ($this->_segments as $segment) {
$this->_uploaded_url .= $segment . DIRECTORY_SEPARATOR;
}
BaseFileHelper::createDirectory($image_dir . $this->_uploaded_url, 0775, true);
$this->_uploaded_url .= md5($this->_image_name . time()) . '.' . $this->imageFile->extension;
$this->imageFile->saveAs($image_dir . $this->_uploaded_url);
return true;
} else {
return false;
}
}
示例11: actionFileUploadAvatar
public function actionFileUploadAvatar()
{
if (Yii::$app->request->isPost) {
$id = Yii::$app->user->id;
$model = User::findOne($id);
$path = Yii::getAlias("@frontend/web/images/users/");
BaseFileHelper::createDirectory($path);
$model->scenario = 'view';
$file = UploadedFile::getInstance($model, 'imgsource');
$name = $file->baseName . '.' . $file->extension;
$file->saveAs($path . DIRECTORY_SEPARATOR . $name);
$model->imgsource = $name;
$model->save();
return true;
}
return false;
}
示例12: insert
/**
* create book.
* @return Book|null the saved model or null if saving fails
*/
public function insert()
{
$this->preview = UploadedFile::getInstance($this, 'preview');
if ($this->validate()) {
$book = new Book();
$book->name = $this->name;
$book->date = strtotime($this->date);
$book->author_id = $this->author_id;
if ($book->save()) {
// если сохранило сообщение, то дописываем файл ------------------------------
$dir = Yii::getAlias(Yii::$app->params['previewPath']);
// если надо - создаем каталог
if (!is_dir($dir)) {
BaseFileHelper::createDirectory($dir, 0777);
}
$uploaded = false;
$filename = '';
if ($this->preview) {
$filename = 'b' . $book->book_id . 'preview.' . $this->preview->extension;
$uploaded = $this->preview->saveAs($dir . '/' . $filename);
// урезаем размер файла, что б не грущили 100500мегабайт и пересохраняем
$img = Image::getImagine()->open($dir . '/' . $filename);
$size = $img->getSize();
$ratio = $size->getWidth() / $size->getHeight();
$width = 700;
$height = round($width / $ratio);
Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/' . $filename, ['quality' => 80]);
// делаем превьюшку
$ratio = $size->getWidth() / $size->getHeight();
$width = 200;
$height = round($width / $ratio);
Image::thumbnail($dir . '/' . $filename, $width, $height)->save($dir . '/th_' . $filename, ['quality' => 80]);
}
// если файл залился - пишем в базу данные по файлу
if ($uploaded) {
$bookData = Book::findOne($book->book_id);
$bookData->preview = $filename;
$bookData->save();
}
return $book;
}
}
return null;
}
示例13: actionFileUploadImages
public function actionFileUploadImages()
{
if (Yii::$app->request->isPost) {
$id = Yii::$app->request->post("advert_id");
$path = Yii::getAlias("@frontend/web/uploads/adverts/" . $id);
BaseFileHelper::createDirectory($path);
$file = UploadedFile::getInstanceByName('images');
$name = time() . '.' . $file->extension;
$file->saveAs($path . DIRECTORY_SEPARATOR . $name);
$image = $path . DIRECTORY_SEPARATOR . $name;
$new_name = $path . DIRECTORY_SEPARATOR . "small_" . $name;
$size = getimagesize($image);
$width = $size[0];
$height = $size[1];
Image::frame($image, 0, '666', 0)->crop(new Point(0, 0), new Box($width, $height))->resize(new Box(1000, 644))->save($new_name, ['quality' => 100]);
sleep(1);
return true;
}
}
示例14: attachImage
public function attachImage($absolutePath, $isMain = false)
{
if (!preg_match('#http#', $absolutePath)) {
if (!file_exists($absolutePath)) {
throw new \Exception('File not exist! :' . $absolutePath);
}
}
if (!$this->owner->id) {
throw new \Exception('Owner must have id when you attach image!');
}
$pictureFileName = substr(md5(microtime(true) . $absolutePath), 4, 6) . '.' . pathinfo($absolutePath, PATHINFO_EXTENSION);
$pictureSubDir = $this->getModule()->getModelSubDir($this->owner);
$storePath = $this->getModule()->getStorePath($this->owner);
$newAbsolutePath = $storePath . DIRECTORY_SEPARATOR . $pictureSubDir . DIRECTORY_SEPARATOR . $pictureFileName;
BaseFileHelper::createDirectory($storePath . DIRECTORY_SEPARATOR . $pictureSubDir, 0775, true);
copy($absolutePath, $newAbsolutePath);
if (!file_exists($absolutePath)) {
throw new \Exception('Cant copy file! ' . $absolutePath . ' to ' . $newAbsolutePath);
}
if ($this->modelClass === null) {
$image = new models\Image();
} else {
$image = new ${$this->modelClass}();
}
$image->itemId = $this->owner->id;
$image->filePath = $pictureSubDir . '/' . $pictureFileName;
$image->modelName = $this->getModule()->getShortClass($this->owner);
$image->urlAlias = $this->getAlias($image);
if (!$image->save()) {
return false;
}
if (count($image->getErrors()) > 0) {
$ar = array_shift($image->getErrors());
unlink($newAbsolutePath);
throw new \Exception(array_shift($ar));
}
$img = $this->owner->getImage();
if (is_object($img) && get_class($img) == 'pistol88\\gallery\\models\\PlaceHolder' or $img == null or $isMain) {
$this->setMainImage($image);
}
return $image;
}
示例15: upload
public function upload()
{
if ($this->validate()) {
$imagable = \Yii::$app->shop_imagable;
$dir = $imagable->imagesPath . '/shop-product/';
if (!empty($this->image)) {
if (!file_exists($dir)) {
BaseFileHelper::createDirectory($dir);
}
$newFile = $dir . $this->image->name;
if ($this->image->saveAs($newFile)) {
$image_name = $imagable->create('shop-product', $newFile);
unlink($newFile);
return $image_name;
} else {
throw new Exception('Image saving failed.');
}
}
}
return false;
}