ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -
i running os ububtu 16.04. ultimate goal able run php script cron job every minute. here php script located @ /var/www/html/tests/test/index.php:
<?php $directorypath = $_server['document_root']."/tests/test/data/"; echo "directorypath: $directorypath<br><br>";//check $imagefilepathsarray = glob($directorypath . "*.{png,gif}", glob_brace); echo "<br><br>imagefilepathsarray: "; print_r($imagefilepathsarray);//check ?> when open php script in browser visiting url my-ip-address/tests/test/index.php, get:
directorypath: /var/www/html/tests/test/data/ imagefilepathsarray:array( [0] => /var/www/html/tests/test/data/pic.png [1] => /var/www/html/tests/test/data/pic.gif ) before writing cron job in crontab file, need check if can run anywhere using terminal. opened ubuntu terminal @ /home/ location, , executed in terminal:
sudo php /var/www/html/tests/test/index.php i got following output:
directorypath: /var/www/html/tests/test/data/ imagefilepathsarray: array ( ) the question why empty array when same script run command line, whereas populated array when run browser?
you can't use $_server['document_root'] when you're running script through cli.
$_server['document_root'] (just $_server-params) populated web server. when you're running script through cli, web server never involved , thus, parameter won't set.
Comments
Post a Comment