简体   繁体   中英

Executing shell command in php doesn't work

I'm trying to execute a shell script to convert some images from .png to .tif using the command convert.

I have written a short shell script:

#! /bin/bash
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/1.png /Applications/MAMP/htdocs/test/1.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/2.png /Applications/MAMP/htdocs/test/2.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/3.png /Applications/MAMP/htdocs/test/3.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/4.png /Applications/MAMP/htdocs/test/4.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/5.png /Applications/MAMP/htdocs/test/5.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/6.png /Applications/MAMP/htdocs/test/6.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/7.png /Applications/MAMP/htdocs/test/7.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/8.png /Applications/MAMP/htdocs/test/8.tif
convert -adaptive-resize 150% /Applications/MAMP/htdocs/test/9.png /Applications/MAMP/htdocs/test/9.

If I run this script from the terminal it works as expected.

Instead if I launch this from whithin a php page it doesn't.

I'm using this code:

exec("bash /Applications/MAMP/htdocs/test/convertpngtif.sh");

How can I solve this?

This is a permissions problem. You need to allow the user that PHP runs as to have access to the sh file.

PHP usually runs as www-data, that being the case you can open a terminal and run something like:-

chown www-data:www-data /Applications/MAMP/htdocs/test/convertpngtif.sh

I don't know how your system is set up, so you may have to sudo that command.

You will have to make sure that www-data also has access to the files accessed by the bash script in a similar way, so just the following command may work:-

chown -R www-data:www-data /Applications/MAMP/htdocs/test/

解决方案:在shell脚本中使用完整路径。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM