Running linux shell script with PHP-ThrowExceptions

Exception or error:

I have created shell script:

#!/bin/bash
file="/home/upload/ftp/$1.ifc"
if [ -f "$file" ]
then
    echo "Found."
else
    echo "Not found."
fi
IfcConvert --use-element-guids /home/upload/ftp/${1}.ifc /home/upload/ftp/${1}.dae

When I go to linux terminal and execute it from there with

./script.sh drevodomek

everything works fine.. I got “Found.” message and also converter executes

But when I created helper.php

<?php
if (file_exists("/home/upload/ftp/drevodomek.ifc"))  
{ 
    echo "The file exists"; 
} 
else 
{ 
    echo "The file does not exists"; 
}

$cmd = "./script.sh drevodomek";
$results = shell_exec($cmd);
echo $results
?>

I get answers:

“The file exists.” from PHP,

“Found.” from script.sh

IfcConvert: input file “/home/upload/ftp/drevodomek.ifc” doesn’t exists. from script.sh

I am new to linux, has anyone any idea what is the difference running script from terminal and with shell_exec()? Thanks!

How to solve:

The problem was with the rights, seems like terminal was run as root, but php exetuted script as normal user. Always check your 777!

Leave a Reply

Your email address will not be published. Required fields are marked *