I have already searched for this issue. But the task was different on each question/blog. So those answers or solution are not relevant to me.
First, here my task details.
User uploads a CSV file in a request. I'm storing that file using laravel's filesystem (local driver).
$path = $request->file('csv_file')->storeAs('requests/' . $userRequest->id, 'input.csv');
So that CSV file will be stored as storage/app/requests/{$userRequest->id}/input.csv
. I have checked owner of requests
directory, and it is www-data
.
Now I have set some artisan command in cronjobs to process the user requests. And I have to store output of the process in that request directory i.e, storage/app/requests/{$userRequest->id}/output/output_1.csv
.
$file = 'requests/' . $userRequest->id . '/output/' . $outputfilename . '.csv';
\Storage::put($file, $content);
But while creating output file, it throws error
Impossible to create the root directory
I know the reason. It is because of the owner who runs artisan command. Artisan command is run by another user (akshay
), while requests
directory is created by www-data.
So here are possible ways to fix this according to me.
akshay
as owner.Many users have suggested changing the owner of the directory. e.g, this one. But as we can see, in my case, the creation of directories is fully dynamic. So I can't do that. But I don't know how to do any of the above with laravel.
I would prefer option (2) because only "request file uploading" will be done by the www-data
user. All other executions will be done by akshay
user. e.g, I have to upload file only one time. But I have to store process output for 500 times.
If anyone knows the answer, it will be appreciated.
After struggling my head all the day with this, I decided to use the easy solution:
Create the cronjob for the www-data user
sudo -u www-data cronjob -e
I hope it helps, I really could not find a best solution when working with cron jobs, artisan commands and directories.