Xianoth Posted October 9, 2007 Share Posted October 9, 2007 If this is in the wrong spot, I apologize ahead of time.... I wrote a PHP script and I am trying to have it execute in a bash environment. I have verified that the PHP5-CGI is properly installed. However, when I try to execute this script, all it is doing is echoing back the full script to my terminal screen. I know I must be missing something simple. Please help! Code follows: #!/usr/bin/php5-cgi -q //Sets working directory $dir = /var/www/test //Scans the working directory and puts all information into an array function select_files ($dir) { if (is_dir($dir)) { if ($handle = opendir($dir)) { $files = array(); while (false !== ($file = readdir($handle))) { if (is_file($dir.$file) && $file != basename ($_server['PHP_SELF"])) $file [] = $file; } closedir ($handle); if (is_array ($files)) sort ($files); return $files; } } } //Connect to database, assumes database and tables exist mysql_connect ("localhost", "username", "password") or die(mysql_error()); mysql_select_db ("dbtest") or die(mysql_error()); //Insert directory array into Database function insert_record ($name, $mod_date); $sql = sprintf ("INSERT INTO dbtest SET filename = '%s', lastdate = '%s', location = '%s', $name, $mod_date, $fdir); if (mysql_query ($sql)) { return true; } else { return false; } Also, any advice on the code will be appreciated as well. Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/ Share on other sites More sharing options...
MmmVomit Posted October 9, 2007 Share Posted October 9, 2007 I've never tried doing it that way, but have you tried putting in php tags? #!/usr/bin/php5-cgi -q <?php // your code ?> Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/#findComment-365447 Share on other sites More sharing options...
Xianoth Posted October 9, 2007 Author Share Posted October 9, 2007 can't say that I have.. I will try it and see if that works.. Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/#findComment-365449 Share on other sites More sharing options...
trq Posted October 9, 2007 Share Posted October 9, 2007 You also have an error. This... $dir = /var/www/test should be... $dir = '/var/www/test'; Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/#findComment-365510 Share on other sites More sharing options...
Xianoth Posted October 9, 2007 Author Share Posted October 9, 2007 Ok I have modded a few things in the script and I have gotten along a lil farther. I am now getting a parse error on line 38, which is the end of the script itself. Not quite sure why this error is being flagged at me.. Here is the modified code: #!/usr/bin/php5-cgi -q //Sets working directory $dir = '/var/www/test'; //Scans the working directory and puts all information into an array function select_files ($dir) { if (is_dir($dir)) { if ($handle = opendir($dir)) { $files = array(); while (false !== ($file = readdir($handle))) { if (is_file($dir.$file) && $file != basename ($_server['PHP_SELF"])) $file [] = $file; } closedir ($handle); if (is_array ($files)) sort ($files); return $files; } } } <?php //Connect to database, assumes database and tables exist mysql_connect ("localhost", "root", "root1234") or die(mysql_error()); mysql_select_db ("dbtest") or die(mysql_error()); //Insert directory array into Database function insert_record ($name, $mod_date){ $sql = sprintf ("INSERT INTO dbtest SET filename = '%s', lastdate = '%s', location = '%s', $name, $mod_date, $fdir); if (mysql_query ($sql)) { return true; } else { return false; } } ?> end I placed the php tags around the actual php code I am trying to use to insert the information into the database. Pointers and suggestions appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/#findComment-365516 Share on other sites More sharing options...
MmmVomit Posted October 9, 2007 Share Posted October 9, 2007 #!/usr/bin/php5-cgi -q <?php //Sets working directory $dir = '/var/www/test'; //Scans the working directory and puts all information into an array function select_files ($dir) { if (is_dir($dir)) { if ($handle = opendir($dir)) { $files = array(); while (false !== ($file = readdir($handle))) { if (is_file($dir.$file) && $file != basename ($_server['PHP_SELF"])) $file [] = $file; } closedir ($handle); if (is_array ($files)) sort ($files); return $files; } } } //Connect to database, assumes database and tables exist mysql_connect ("localhost", "root", "root1234") or die(mysql_error()); mysql_select_db ("dbtest") or die(mysql_error()); //Insert directory array into Database function insert_record ($name, $mod_date){ $sql = sprintf ("INSERT INTO dbtest SET filename = '%s', lastdate = '%s', location = '%s', $name, $mod_date, $fdir); if (mysql_query ($sql)) { return true; } else { return false; } } ?> end Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/#findComment-365584 Share on other sites More sharing options...
trq Posted October 9, 2007 Share Posted October 9, 2007 if (is_file($dir.$file) && $file != basename ($_server['PHP_SELF"])); $file [] = $file; Quote Link to comment https://forums.phpfreaks.com/topic/72475-problems-executing-code/#findComment-365587 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.