I'm trying to implement php script on server created by someone else. I try echo shell_exec('ls');
and it works.
var_dump(shell_exec('find / -name "tmp.txt"'));
returns null. However when I try it in ssh console, the output is correct. What can cause it? What can I check
You're trying to locate something in the root directory, which your web server doesn't have permissions to access (by design) - what you're wanting to do would be a security concern, so my suggestion is to place your work in a folder inside your website, owned by the same user that is allowed to access files on your drive, typically www-data
.
Perhaps you don't want to read the root directory of the server itself, and you just want to read a file in the same directory as the website. Switching out the /
for a .
will access the current directory, not the base directory:
var_dump(shell_exec('find . -name "tmp.txt"'));
Or, you could try an absolute path:
var_dump(shell_exec('find /path/to/files -name "tmp.txt"'));
This is uncommon, but you may need to point to an absolute path for find
as well. You can test if you need to do this by running var_dump(shell_exec('find .'));
and if it has no output then you will probably have to. Use /usr/bin/find
instead if this is the case.
Read more about how to set permissions
Apache: File and Directory Ownership and Permissions for Web Content
A reason for this can be a missing PATH environment variable.
Please check getenv('PATH')
and try to use the absolute path to find. A usual place is /usr/bin/find or /bin/find.
var_dump(shell_exec('/usr/bin/find / -name "tmp.txt"'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.