I'm running the following script for an image upload in a server and getting the following error while it works perfectly on the localhost.
Code
$user_id = Auth::id();
$logicpath = 'userdp/' . $user_id . '/';
$pubpath = 'userdp/' . $user_id . '/' . $dpFile;
$path = '/userdp/' . $user_id . '/' . $dpFile;
if (!file_exists($logicpath)) {
mkdir($logicpath, 0777, true);
}
Error
ErrorException in UploadController.php line 605: mkdir(): Permission denied
at HandleExceptions->handleError('2', 'mkdir(): Permission denied', '/var/www/html/laravel/app/Http/Controllers/UploadController.php', '605', array('dp' => object(UploadedFile), 'ext' => 'jpg', 'img' => object(Image), 'mime' => 'image/jpeg', 'width' => '200', 'height' => '200', 'fileSize' => '17152', 'dpFile' => 'f12f298ab18d58a59c4ed8a589cd1cdc.jpg', 'user_id' => '1', 'logicpath' => 'userdp/1/', 'pubpath' => 'userdp/1/f12f298ab18d58a59c4ed8a589cd1cdc.jpg', 'path' => '/userdp/1/f12f298ab18d58a59c4ed8a589cd1cdc.jpg'))
I tried chmod 777 public
and restarted the server. But it didn't work.
You have to perform chmod recursively to make sure all subdirectories also carry the same permissions. Try running this instead:
chmod -R 777
Now while this will fix your issue, you should definitely read up on the problems that arise with setting the above permissions as this link posted in comments below shows. Ill post here as well:
http://stackoverflow.com/a/11271632/2790481
EDIT: This should only be done within the Laravel storage dir within /var/www/html/laravel/storage
, for example and never be done at the system level. I forgot to mentioned that originally, hence all the downvotes.
I tried chmod 777 public and restarted the server. But it didn't work.
Be sure you use the CHMOD recursively and with administrator permissions (if you can)
SUDO CHMOD -R 777 /var/www/html/laravel/
Regards
You are trying to move the uploaded file a folder in the root of your server. Make sure you get the absolute path right.
$logicpath = public_path() . '/userdp/' . $user_id . '/';
Same error for the command
composer create-project --prefer-dist laravel/laravel MyProject
But it worked after the following commands
sudo mkdir MyProject
sudo chmod -R 777 MyProject
Or better run composer create-project command to some different path than htdocs
Try:
sudo chown -R www-data:www-data /var/www/yoursite/public
I have found a very interesting solution to this problem. Just put "." sign like this example. It works for me.
$destinationPath = "./public/uploads"; // upload path
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0755, true);
}
$request->sub_category_attr_value->move($destinationPath, $picName);