问题
I am writing a PHP script for the GUI of a Cent OS server. For testing purposes I have set up the Apache as localhost. Cent OS does not automount a connected USB device. In PHP I need to check if a USB device is plugged in then mount it and list the file content for the user. This is quite simple in bash, however I am unable to make it work in PHP. I have played around with both the exec()
and the shell_exec()
functions with no avail. I have tried the commands both manually i.e.:
shell_exec("sudo mount /dev/sdb1 /mnt");
and through a bash script:
exec("./mountlist.sh");
Is there a function in PHP I can call that will do the same thing, or am I missing something with the exec
and shell_exec
functions. Both functions work fine in the shell. Since mount is a root command I included sudo
in the bash script.
回答1:
You can't sudo from inside a PHP script - there is no way to type in the sudo password.
You could create a shell script and use the STICKY bit to have it run as the root user
http://www.dba-oracle.com/linux/sticky_bit.htm
That's how the passwd command can write to the password file owned by root even though you are running it as a normal user.
Also - you could grant the web user permission to mount/unmount file systems (add him to the fuse group on most systems), but that's more open-ended and rather dangerous if your web server gets hacked, so I would go with shell scripts and sticky bits for your purposes.
来源:https://stackoverflow.com/questions/12147245/php-mount-usb-device