2011-11-14 45 views
0

我需要製作一個網站,人們可以上傳數據文件,經過一些處理後將使用jpgraph進行繪圖。該文件使用名爲analfile.sh的bash腳本進行分析。 bash腳本是這樣的:fopen動態文件名不起作用

#!/bin/bash 

file=$1 
fecha=`date +%s` 
mv $1 $fecha.dat 
echo $fecha.dat 

因此,它給出了另一個文件,其名稱是這樣的:1321290921.dat。這是我需要繪製的文件。

這是我當前的PHP代碼:

$target_path = "/home/myhome"; 
$target_path = $target_path . basename($_FILES['rdata']['name']); 
$target_file = basename($_FILES['rdata']['name']); 

if(move_uploaded_file($_FILES['rdata']['tmp_name'], $target_path)) { 
    echo "The file ". $target_file. " has been uploaded";  
    chdir('/home/myhome');  
    $filetoplot=shell_exec('./analfile.sh'.' '.$target_file);  
} else{  
    echo "There was an error uploading the file, please <a href=\"index.html\">try again! 
</a>"; 
} 

//$filetoplot="1321290921.dat" 
echo "<br>The file to be opened is ". $filetoplot. "<br>"; 

if ($file_handle = fopen("$filetoplot", 'r')) {  
    while ($line_of_text = fgets($file_handle)) { 
    $parts = explode('.', $line_of_text); 
    echo $line_of_text ; 
    print $parts[0] . $parts[1]. $parts[2]. "<br>"; 
    } 
fclose($file_handle);  
} 

我有權讀取並在目標目錄寫。我發現奇怪的是,如果我取消註釋行$ filetoplot =「1321290921.dat」,那麼腳本完美。我想我正在做一些愚蠢的事情,因爲這是我在php中的第一個代碼,但經過幾個小時的搜索後,我無法找到解決方案。

任何幫助將不勝感激。

+1

仔細檢查您是否未在安全模式下運行PHP。 –

回答

0

我看到的第一件事是,您不要在您的主路徑中追加尾部斜槓(/),以便路徑將如/home/myhomefoo而不是/home/myhome/foo

您還應該提前移動$target_file,並在$target_path之內重新使用它。沒有理由做同樣的事情兩次。

如果這沒有幫助,我們會看看接下來會發生什麼。