Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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);
$.ajax({
    url: 'addimagegallery',
    type: 'POST',
    data: {addimagegallery: 'addimagegallery'},
.done(function(response) {
    console.log(response);
                you might be interesting in this also stackoverflow.com/questions/30682421/… where you create your images in already writable folder.
– Maytham Fahmi
                Aug 26, 2015 at 15:38

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); 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

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, 2014 at 1:57 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, 2014 at 1:59 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, 2020 at 8:55

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')
                Do you know why when the new directory is written on cyrillic, in the filesystem the dir name looks like this: Адлл
– Alex
                Jan 8, 2016 at 16:45
                @Alex Probably because of font and/or encoding problems. Something along the lines of unix.stackexchange.com/questions/16771/…
– Hop hop
                Feb 25, 2016 at 5:36

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);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.