简体   繁体   English

使用PHP运行命令行应用程序

[英]Running commandline applications with PHP

How can I successfully run commandline applications from PHP on a linux/Mac system. 如何在Linux / Mac系统上从PHP成功运行命令行应用程序。

I'm using a mac and trying to run optipng. 我正在使用Mac,并尝试运行optipng。 I've downloaded optipng from here 我已经从这里下载了optipng

http://sourceforge.net/projects/optipng/files/OptiPNG/optipng-0.7.5/optipng-0.7.5.tar.gz/download?use_mirror=softlayer-sng&download=

After downloading it, I installed it via the command line by CD'ing to the download folder, and installing it accordingly: 下载后,我通过命令行将CD安装到下载文件夹中,并进行了相应的安装:

cd path/to/optipng
sudo ./configure
sudo make install

I can now run it and compress images like this: 我现在可以运行它并压缩图像,如下所示:

optipng path/to/example/file.png

Everything works fine so it's installed and working on my system. 一切正常,因此可以在我的系统上安装并正常工作。

I want to run it via a php page so i try this: 我想通过php页面运行它,所以我尝试这样做:

<?php
echo "origional size =". filesize(path/to/example/file.png)."\n";

$compress = shell_exec('optipng path/to/example/file.png');

echo "<pre>$compress</pre>";

echo "new size =". filesize(path/to/example/file.png)."\n";

?>

But it does nothing. 但是它什么也没做。 What am I missing here? 我在这里想念什么? How do I use shell_exec() or exec() successfully? 如何成功使用shell_exec()或exec()?

<?php
exec('/dev/null > /tmp/optipng.log');
echo "origional size =". filesize('/tmp/steve-jobs.png')."\n";

shell_exec('/usr/local/bin/optipng -log /tmp/optipng.log /tmp/steve-jobs.png');
$compress = file_get_contents('/tmp/optipng.log');

echo "<pre>$compress</pre>";

echo "new size =". filesize('/tmp/steve-jobs.png')."\n";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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