Change uploaded file permissions

I'm uploading a file (ssh_key) with a FileUpload element, in my Resource. However, I would like to make sure the permissions for the uploaded file are 0600, but I'm not being able to do so with "afterStateUpdated" method, which I though was the way to go, like this:
->afterStateUpdated(function ($state) {
if ($state instanceof UploadedFile) {
$filePath = $state->hashName();

if (Storage::disk('ssh_keys')->exists($filePath)) {
$absolutePath = Storage::disk('ssh_keys')->path($filePath);
chmod($absolutePath, 0600);
}
}
})
->afterStateUpdated(function ($state) {
if ($state instanceof UploadedFile) {
$filePath = $state->hashName();

if (Storage::disk('ssh_keys')->exists($filePath)) {
$absolutePath = Storage::disk('ssh_keys')->path($filePath);
chmod($absolutePath, 0600);
}
}
})
This example fails to set the file permissions, since it seems not to be able to find it. What would be the best approach to achieve this? "ssh_keys" disk is a local folder, inside the app directories.
4 Replies
awcodes
awcodes7d ago
Why do you need to change the directory permission, if it’s in local storage it’s not publicly accessible anyway.
titoshadow
titoshadow6d ago
Since its an ssh key, I wouldn't be able to use it if its not 0600
awcodes
awcodes6d ago
Is chmod() even a php command? Might be possible too that your server user doesn’t have the permission to change permissions at the server level. Not sure.
titoshadow
titoshadow5d ago
Do you really intend to be useful or are just commenting by the sake of it ? No offense, but first you question my attempt, then continue randomly questioning. Yes, chmod is a PHP command, you can check this by yourself. Finally, I have solved this issue by workaround, setting the file permissions before using it, which is not ideal, but works. From what I've been able to investigate, it seems "local" storage has this kind of issues with visibility and permissions set. I think I'm opening an issue on github.
Want results from more Discord servers?
Add your server