20

I'm uploading files in my system and it works locally where am using windows and xampp but when hosting where am using an Ubuntu stack my file is not being uploaded. I'm getting an error that it cannot be written in the 'system' directory which is within the public folder. This is my code

 $destinationPath = public_path().'/system'; // upload path
  $extension = Request::file('add_students')->getClientOriginalExtension(); // getting excel extension
  // dd($extension);
  $fileName = rand(11111,99999).'.'.$extension; // renameing excel file
  $file=Request::file('add_students')->move($destinationPath, $fileName); // uploading file to given path
  $path=$file->getRealPath();

after looking for solutions I found out that I should make my directory writable so I ran the following command in putty

chmod 770 /var/www/html/public/system

but still even after that am getting an error Unable to write in the "/var/www/html/public/system" directory.

I'm using laravel 5 and my host is digital ocean. Thanks

user3714932
  • 1,253
  • 2
  • 16
  • 29

4 Answers4

52

chmod 755 /var/www/html/public/system and chown www-data:www-data /var/www/html/public/system as stated by @JLPuro works perfectly. Thanks a lot guys.

user3714932
  • 1,253
  • 2
  • 16
  • 29
  • 9
    One might want to add -R flag to do this recursively. Didn't work for me until I added the flag. **chown -R www-data:www-data /path/to/dir** – andromeda Sep 25 '17 at 22:29
4

permission for folder sudo chmod 777./(folder name)

for file sudo chmod 777 -R ./(file name)

RKRK
  • 1,284
  • 5
  • 14
  • 18
  • 3
    This will be disaster, don't use 777 please .This gives permission to everyone read, write even delete. This command actually feed a hacker by you. – Saddan Nov 14 '20 at 14:48
4

if you are using Centos-7 Then use

sudo find directory_name -type f -exec chmod 644 {} \;
sudo find directory_name -type d -exec chmod 775 {} \;

sudo chown -R apache:apache var/www/html/directory_name
chcon -R -t httpd_sys_rw_content_t directory_name

Where directory_name is the main directory of you Project.

0

Whenever you have to create a directory use this:

use File;
use Illuminate\Foundation\Bus\DispatchesJobs;

use this in controller

if (!file_exists(public_path().'system')) {            
                if(File::makeDirectory(public_path().'system',0777,true)){                
                  }
                }

use this in your function