Monday, 7 June 2021

Laravel File system

 NOTE: If web server is apache2, for script execution, file creation, deletion, Laravel uses apache2 default user www-data and default group www-data.

Granting file with www-data group or user read permission will allow laravel to access. Or grant file public with read permission.

NOTE: There is unexception, if laravel creates a folder under a parent folder like HOME/usrTest, and if usrTest/ folder  has default permission that needs newly created file or folder to inheirt, like sometimes it will be usr:usr owner and group, and some default permission, laravel will in herit instead of creating www-data:www-data with specified permission in make makeDirectory($path, 0755, true, true). This can be found out by just using touch HOME/usrTest/testFile.txt, then ll testFile.txt to see its permission in the folder.


NOTE: for web application to read file, the file needs permission for www-data

NOTE: for Laravel web application to create/delete file speficifed in a path, no permission is concered, as Laravel will use the default user/group and permission required to inheirt from parent folder.

NOTE: for Laravel / PHP upload to work, need to check php.ini upload_max_filesize .

NOTE: When file is first uploaded, it is located in upload_tmp_dir in php.ini.

NOTE: php.ini location is /etc/php/7.4/fpm/phpini .

NOTE: apache2 only allows access to /var/www/

so if file upload saved to /home/uploads/file1.txt, web server / web application can not access it.

To fix that either change /etc/apache2/apache2.conf, allow directory access to /home

or for safety create sym link :

https://www.blogger.com/blog/post/edit/2746942211977437381/162920663052388200



use Illuminate\Support\Facades\File;

use Illuminate\Support\Str;


// Create uuid

 (string) Str::uuid();

// Make dir based on Path.

// NOTE: directory's permission for owner and group will be what ever inherited from parent directory.

// If parent directory has owner, group and permission specified, it will be inherited, specifying 0755 won't work.

 $path = '/home/myfolder1/'


// 0755 is folder permissions

// 1st true is recursive creation of folders

// 2nd true is to force it to do it

//https://stackoverflow.com/questions/38104912/laravel-5-2-storagemakedirectorydir-is-not-creating-directory

// If parent directory has owner, group and permission specified, it will be inherited, specifying 0755 won't work.

File::isDirectory($path) or File::makeDirectory($path, 0755, true, true);

// Get File and move file

   $file = Input::file('file');

// Note must get file extension before move or else there will be error because file will no longer be in php tmp fodler

 $fileServerName = $file_uuid . '.' . $file->getClientOriginalExtension();

 $file = Input::file('file');

 $file->move($path, $fileServerName);

// Delete

// https://stackoverflow.com/questions/33842735/how-to-delete-file-from-public-folder-in-laravel-5-1

// no concern of permission

File::delete($path)

// PHP method unlink()

unlink($image_path);



No comments:

Post a Comment