php 在laravel中创建文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21869223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create folder in laravel
提问by user1775888
I have problem let user create folder in laravel 4 through ajax request > route > controller@method.
I did test ajax success request to the url call right method.
When I use mkdiror File::mkdir($path);(is this method exist?) , I will get the response Failed to load resource: the server responded with a status of 500 (Internal Server Error)and fail to create new folder.. how to solve it ?
我有问题让用户通过 ajax request > route > controller@method 在 laravel 4 中创建文件夹。
我确实测试了对 url 调用正确方法的 ajax 成功请求。
当我使用mkdiror File::mkdir($path);(这个方法是否存在?)时,我会得到响应Failed to load resource: the server responded with a status of 500 (Internal Server Error)并且无法创建新文件夹..如何解决?
route.php
路由文件
Route::post('admin/article/addimagegallery', 'AdminDashboardController@addImagegallery');
AdminDashboardController
管理仪表板控制器
public function addImagegallery()
{
if (Request::ajax())
{
…
$galleryId = 1; // for test
$path = public_path().'/images/article/imagegallery/'.$galleryId;
File::mkdir($path);
}
}
js
js
$.ajax({
url: 'addimagegallery',
type: 'POST',
data: {addimagegallery: 'addimagegallery'},
})
.done(function(response) {
console.log(response);
});
回答by The Alpha
No, actually it's
不,实际上是
File::makeDirectory($path);
Also, you may try this:
另外,你可以试试这个:
$path = public_path().'/images/article/imagegallery/' . $galleryId;
File::makeDirectory($path, $mode = 0777, true, true);
Update:Actually it does work, mkdiris being used behind the scene. This is the source:
更新:实际上它确实有效,mkdir正在幕后使用。这是来源:
/**
* Create a directory.
*
* @param string $path
* @param int $mode
* @param bool $recursive
* @param bool $force
* @return bool
*/
public function makeDirectory($path, $mode = 0777, $recursive = false, $force = false)
{
if ($force)
{
return @mkdir($path, $mode, $recursive);
}
else
{
return mkdir($path, $mode, $recursive);
}
}
For deleting:
对于删除:
public function deleteDirectory($directory, $preserve = false);
Check the source at following path (in your local installation):
检查以下路径中的源代码(在您的本地安装中):
root/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php
root/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php
回答by JohnWolf
Thanks to The Alpha. Your answer helped me, here is a laravel 5 way to do it for those who use the later version :
感谢阿尔法。您的回答对我有所帮助,这是为那些使用更高版本的人提供的 laravel 5 方法:
Storage::disk('local')->makeDirectory('path/to');
This will create a directory in storage/app/path/to
这将创建一个目录 storage/app/path/to
Retrieve the directory you just created with :
检索您刚刚创建的目录:
storage_path('app/path/to')
回答by Bijaya Kumar Oli
There's multiple arguments you can use.
您可以使用多个参数。
You can create a directory using defaults.
您可以使用默认值创建目录。
$result = File::makeDirectory('/path/to/directory');
This will return true if directory was able to be created in the /path/to directory. The file mode of the created directory is 0777.
如果能够在 /path/to 目录中创建目录,这将返回 true。创建的目录的文件模式为0777。
You can specify the mode.
您可以指定模式。
$result = File::makeDirectory('/path/to/directory', 0775);
This will return true if directory was able to be created in the /path/to directory. The file mode of the created directory will be 0775.
如果能够在 /path/to 目录中创建目录,这将返回 true。创建的目录的文件模式将为 0775。
You can also create the directories recursively.
您还可以递归地创建目录。
$result = File::makeDirectory('/path/to/directory', 0775, true);

