2014-02-19 85 views
11

我有问题,让用户通过ajax请求>路由>控制器@方法在laravel 4中创建文件夹。
我没有测试ajax成功请求的URL调用权的方法。
当我使用mkdirFile::mkdir($path);(这种方法是否存在?)时,我将得到响应Failed to load resource: the server responded with a status of 500 (Internal Server Error)并且无法创建新文件夹..如何解决它?在laravel中创建文件夹

route.php

Route::post('admin/article/addimagegallery', '[email protected]'); 

AdminDashboardController

public function addImagegallery() 
{ 
    if (Request::ajax()) 
    { 
     … 
     $galleryId = 1; // for test 
     $path = public_path().'/images/article/imagegallery/'.$galleryId; 
     File::mkdir($path); 
    } 
} 

JS

$.ajax({ 
    url: 'addimagegallery', 
    type: 'POST', 
    data: {addimagegallery: 'addimagegallery'}, 
}) 
.done(function(response) { 
    console.log(response); 
}); 
+0

你可能会在这有趣也http://stackoverflow.com/questions/30682421/how-to-protect-image-from-public-view-in-laravel-5/30682456#30682456你在哪里创建你的已写入文件夹中的图像。 –

回答

33

不,实际上它是

File::makeDirectory($path); 

此外,你可以试试这个:

$path = public_path().'/images/article/imagegallery/' . $galleryId; 
File::makeDirectory($path, $mode = 0777, true, true); 

更新:其实它的工作,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); 
    } 
} 

删除:

public function deleteDirectory($directory, $preserve = false); 

请在以下路径(在本地安装)来源:

根/供应商/ laravel /框架/src/Illuminate/Filesystem/Filesystem.php

+0

哇非常感谢!基本的PHP'mkdir'不能和laravel一起工作?以及如何删除文件夹?我无法在laravel中找到该文档 – user1775888

+1

非常感谢!链接是404,但我觉得这个'https:// github.com/laravel/framework/blob/master/src/Illuminate/Filesystem/Filesystem.php – user1775888

+1

@ user1775888:http://laravel.com/api/class- Illuminate.Filesystem.Filesystem.html – kba

3

感谢阿尔法。你的回答帮我,这里是一个laravel 5办法做到这一点对于那些谁使用更高版本:

Storage::disk('local')->makeDirectory('path/to'); 

这将创建storage/app/path/to

目录检索目录,你只是创建:

storage_path('app/path/to') 
+0

你知道为什么当新的目录写成西里尔文时,在文件系统中目录名称如下所示:Радлл – Alex

+0

@Alex可能是由于字体和/或编码问题。 http://unix.stackexchange.com/questions/16771/foreign-characters-wont-display-in-ssh/16784上的内容 –

相关问题