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); });
The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
From Laravel official documentation. The storage directory contains compiled Blade templates, file based sessions, file caches, and other files generated by the framework. This folder is segregated into app, framework, and logs directories. The app directory may be used to store any files utilized by your application.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With