2013-02-04 37 views
-1

我寫了一個php腳本,用於在終端中運行bash腳本。我安裝了PHP5 php-cli和php-pear。 PHP腳本使用瀏覽器在apache服務器上運行平穩,但從終端運行時什麼也不做。我也讓它可以執行所有的讀寫權限。我會把這個bash腳本每5分鐘運行一次cron作業。PHP不會在php-cli的終端中運行,但在Apache服務器上運行

#!/usr/bin/php5 -q 
<?php 
    $file="/var/www/floods/php/indx.txt"; 
    $indx=file_get_contents($file); 
    $con = mysql_connect("localhost","user","password"); 
    if(!$con){die('Could not connect: ' . mysql_error());} 
    mysql_select_db("smsgw",$con); 

    $acc = mysql_query("SELECT * FROM accounts"); 
    while($fld = mysql_fetch_array($acc)){ 
     $index[]=$fld['id']; 
     $num_reg[]=$fld['num']; 
     $loc[]=$fld['location'];  
    } 
    if(isset($index)){ 

    $inbox = mysql_query("SELECT * FROM inbox"); 
    while($fields=mysql_fetch_array($inbox)){ 
     $id[]=$fields['id']; 
     $num_inbox[]=$fields['number']; 
     $smsdate[]=$fields['smsdate']; 
     $text[]=$fields['text'];  
    } 

    $last_indx=count($id); 
    $last_num=count($index); 


    for($i=0;$i<$last_num;$i++){ 
     for($j=(int)$indx;$j<$last_indx;$j++){ 
      if(strcmp($num_inbox[$j],$num_reg[$i])==0){ 
       $file_save= $num_reg[$i] . ".txt"; 
       file_put_contents($file_save,$text[$j]); 
      } 
     } 

    } 

    }/*End*/ 

?> 
+0

你如何調用它?你有任何錯誤? –

+0

它不以任何特定方式運行嗎? –

+0

如果我直接在終端中運行,沒有錯誤。我只是使用'php -f/path/to/file/process.php'。文本文件應該在那之後創建,但是如果它在終端中運行,它不會生成任何文本文件。但是它從Web瀏覽器http://localhost/process.php運行時生成了這些文件,它應該在沒有打開任何瀏覽器的情況下靜默運行,這就是爲什麼我要從終端運行它。 :( –

回答

0

感謝您的意見和幫助。終於解決了這個問題。 我應該指定保存文本文件的確切路徑。 $file_save= $num_reg[$i] . ".txt";應該是這樣的 $file_save="/path/where/to/save". $num_reg[$i] . ".txt";

相關問題