laravel-medialibrary: Error on save when conversion tries to clean up tmp dir
Every time I attach an image the media conversion throws an error. It looks like it’s trying to delete /tmp
to clean up the temp items. What am I doing wrong?
"message": "rmdir(/tmp): Permission denied",
"exception": "ErrorException",
"file": "/var/www/html/ptpkg/vendor/spatie/image/src/GlideConversion.php",
"line": 107,
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 25 (12 by maintainers)
After much digging I’ve found the cause of this issue. I’ve already posted this here in spatie/image#38, but I’ll post it here as well since it’s relative.
It’s related to something called “systemd /tmp isolation”. The php service (and others like httpd) have the PrivateTmp option to true in the systemd script (/usr/lib/systemd/system).
It’s a security measure where a new /tmp is created and isolated from the other ones. All the data saved inside are deleted once the service is stopped.
If running on a Linux system where systemd has PrivateTmp=true (which is the default on CentOS 7 and perhaps other newer distros), sys_get_temp_dir() function will simply return “/tmp”, not the true, much longer, somewhat dynamic path. (example:
systemd-private-5049768e046a4449b2d8dd552949bb17-php-fpm.service-FYJIWf/tmp/
) see: http://php.net/manual/en/function.sys-get-temp-dir.php#121073as a quick fix you can disable the PrivateTmp option. see https://gryzli.info/2015/06/21/centos-7-missing-phpapache-temporary-files-in-tmp-systemd-private-temp/. But since this is a security feature of linux, a better long term solution would be to avoid using the system /tmp directory to cache conversion files, and instead use a directory within the project.
This bug is fixed in
spatie/image ^1.5.1
: https://github.com/spatie/image/releases/tag/1.5.1. More info on how we fixed it: https://github.com/spatie/image/issues/38If you’re using the latest medialibrary version, you can run
composer up
to get the bugfix.I tried this earlier, but somehow it did not work (it still wants to delete /tmp). I’ll try to look at this after work.
@ammonkc @freekmurze @lostincode @ptiuma I came across a similar problem today, and what I found out is that medialibrary is trying to delete a temporary folder only when it’s empty. So if you create for example an empty folder in /tmp dir, everything works as expected.
@Gummibeer thanks for the input. I suggest we move this discussion to the PR: https://github.com/spatie/image/pull/45