Checking exec() runs successfully or not

soft genic picture soft genic · Aug 9, 2012 · Viewed 65.1k times · Source

I have been trying to let know know if the exec() command in php executes successfully or not so i can echo certain messages accordingly. I tried the following piece of code but the problem with it is that whether exec() runs successfully or not it always echo "PDF not created" and never echo pdf created successfully. Kindly let me know how can i perform the check on the execution of exec() so i can echo messages accordingly Thanks,

<?php
if (exec('C://abc//wkhtmltopdf home.html sample.pdf'))
echo "PDF Created Successfully";
else
echo "PDF not created";
?>

Answer

Matt Williamson picture Matt Williamson · Aug 9, 2012

According to PHP's exec quickref, you can pass pointers in to get the output and status of the command.

<?php
exec('C://abc//wkhtmltopdf home.html sample.pdf', $output, $return);

// Return will return non-zero upon an error
if (!$return) {
    echo "PDF Created Successfully";
} else {
    echo "PDF not created";
}
?>

If you want to enumerate the possible errors, you can find the codes over at hiteksoftware