30

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 mkdir or 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 ?

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

$.ajax({
    url: 'addimagegallery',
    type: 'POST',
    data: {addimagegallery: 'addimagegallery'},
})
.done(function(response) {
    console.log(response);
});
user1775888
  • 3,147
  • 13
  • 45
  • 65
  • you might be interesting in this also http://stackoverflow.com/questions/30682421/how-to-protect-image-from-public-view-in-laravel-5/30682456#30682456 where you create your images in already writable folder. – Maytham Fahmi Aug 26 '15 at 15:38

5 Answers5

80

No, actually it's

use File;

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, mkdir is being used behind the scene. This is the source:

/**
 * 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

saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • wow Thanks so much!! does basic php `mkdir` not work with laravel?? and how to delete folder? I can't find the document in laravel – user1775888 Feb 19 '14 at 01:45
  • 1
    Thanks so much! link is 404 but I find this `https://github.com/laravel/framework/blob/master/src/Illuminate/Filesystem/Filesystem.php` – user1775888 Feb 19 '14 at 01:57
  • 1
    Oh no! I meant in your system where the `root` represents the root of of your installation, but you made it anyways, welcome :-) – The Alpha Feb 19 '14 at 01:59
  • 1
    @WereWolf-TheAlpha How I can know if the folder exists before creating it? – TuGordoBello Apr 23 '14 at 15:39
  • How to find this in document? – Phoenix Sep 12 '14 at 03:41
  • 2
    @zhelon: if ( ! File::exists($path) ) { File::makeDirectory($path); } – Rogier Sep 23 '14 at 22:57
  • @TheAlpha: I've tried to create directory in `'public'` folder using above code, but it's not created dir in public folder and also not giving any errors! It's strange!! – Hiren Gohel Apr 20 '18 at 10:19
  • 1
    Thanks for the information. It saved me lots of time with my application on Plesk Ubuntu Server but a small advice would be to use 0755 instead of 0777 for web applications. – Mycodingproject Jan 25 '20 at 08:55
  • yes thank you this is worked for me by use File; $path = 'storage/upload/category/'; File::makeDirectory($path); – saber tabatabaee yazdi Dec 01 '21 at 21:01
18

Thanks to The Alpha. Your answer helped me, here is a laravel 5 way to do it for those who use the later version :

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

This will create a directory in storage/app/path/to

Retrieve the directory you just created with :

storage_path('app/path/to')
JohnWolf
  • 1,147
  • 14
  • 28
  • Do you know why when the new directory is written on cyrillic, in the filesystem the dir name looks like this: Адлл – Alex Jan 08 '16 at 16:45
  • 1
    @Alex Probably because of font and/or encoding problems. Something along the lines of http://unix.stackexchange.com/questions/16771/foreign-characters-wont-display-in-ssh/16784 – Hop hop Feb 25 '16 at 05:36
9

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.

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.

You can also create the directories recursively.

$result = File::makeDirectory('/path/to/directory', 0775, true);
Bijaya Kumar Oli
  • 2,007
  • 19
  • 15
8

You can try this code:-

use Illuminate\Support\Facades\File;

if (! File::exists("your-path")) {
    File::makeDirectory("your-path");
}
Ayman Elshehawy
  • 2,746
  • 23
  • 21
2

for use File this is use \Illuminate\Support\Facades\File

Netwons
  • 1,170
  • 11
  • 14