create folder in laravel

前端 未结 4 1626
迷失自我
迷失自我 2020-12-14 16: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.
Wh

相关标签:
4条回答
  • 2020-12-14 17:01

    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, 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

    0 讨论(0)
  • 2020-12-14 17:03

    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);
    
    0 讨论(0)
  • 2020-12-14 17:04

    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')
    
    0 讨论(0)
  • 2020-12-14 17:06

    You can try this code:-

    use Illuminate\Support\Facades\File;
    
    if (! File::exists("your-path")) {
        File::makeDirectory("your-path");
    }
    
    0 讨论(0)
提交回复
热议问题