PHPs move_uploaded_file does not respect setgid

2019-09-05 15:36发布

问题:

I've moving completed uploads with the function move_uploaded_file. I have noticed this does not respect the target directory's setgid bit and has what the file originally had. (I learnt this is Linux behaviour).

What is a good solution to the problem? I thought about loading the file into PHP and resaving it, but the files can be quite big 50MB+ which means more memory usage for PHP.

回答1:

You may or may not have access to the system call, but a system("cp") or even an explicit chgrp might do what you want, as far as obeying the setgid bit. You might need to do some sleuthing to get the existing group if it varies from target directory to target directory.



回答2:

Try PHP's built in rename instead of move_uploaded_file().



回答3:

I had this problem back in early '13 and fixed it by replacing move_uploaded_file with copy. I am not aware of the actual performance and memory impact, but it seems to be irrelevant.