首先,我聲明變量來獲取文件夾,并將文件放入數組中。見下文 $DOCROOT = $_SERVER['DOCUMENT_ROOT']; $dir = $DOCROOT . '/php/pagamentos/resources/pagamentos'; echo ($dir); $folder = array_diff(scandir($dir), array('.', '..'));我有以下數組要循環:array(4) { [2]=> string(33) "BMEPS_out22002508200201095005.txt" [3]=> string(33) "BMEPS_out22002508200204112009.txt" [4]=> string(33) "BMEPS_out22002508200204125012.txt" [5]=> string(33) "BMEPS_out22002508200205063000.txt"}然后我開始使用以下代碼循環數組foreach ($folder as $file) { $fp = fopen($file, "r") or die("Unable to open file!"); echo $file; fclose($fp);}但畢竟我收到以下錯誤:Warning: fopen(BMEPS_out22002508200204112009.txt): failed to open stream: No such file or directory in /var/www/html/php/pagamentos/index.php on line 40Unable to open file!
1 回答

子衿沉夜
TA貢獻1828條經驗 獲得超3個贊
您缺少文件的完整路徑。當您掃描目錄時,您使用 $dir = $DOCROOT . '/php/pagamentos/resources/pagamentos'; 但當您調用 fopen 時,您只需使用文件名。您必須提供完整路徑。例如:
foreach ($folder as $file) {
$fullPath = $DOCROOT . '/php/pagamentos/resources/pagamentos/' . $file;
//or
$fullPath = $dir . '/' . $file;
//now you use $fullPath instead just $file
}
在僅調用文件名的情況下,腳本會嘗試在執行腳本的同一目錄中打開文件。
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報
0/150
提交
取消