首先,我聲明變量來(lái)獲取文件夾,并將文件放入數(shù)組中。見(jiàn)下文 $DOCROOT = $_SERVER['DOCUMENT_ROOT']; $dir = $DOCROOT . '/php/pagamentos/resources/pagamentos'; echo ($dir); $folder = array_diff(scandir($dir), array('.', '..'));我有以下數(shù)組要循環(huán):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"}然后我開(kāi)始使用以下代碼循環(huán)數(shù)組foreach ($folder as $file) { $fp = fopen($file, "r") or die("Unable to open file!"); echo $file; fclose($fp);}但畢竟我收到以下錯(cuò)誤: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貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
您缺少文件的完整路徑。當(dāng)您掃描目錄時(shí),您使用 $dir = $DOCROOT . '/php/pagamentos/resources/pagamentos'; 但當(dāng)您調(diào)用 fopen 時(shí),您只需使用文件名。您必須提供完整路徑。例如:
foreach ($folder as $file) {
$fullPath = $DOCROOT . '/php/pagamentos/resources/pagamentos/' . $file;
//or
$fullPath = $dir . '/' . $file;
//now you use $fullPath instead just $file
}
在僅調(diào)用文件名的情況下,腳本會(huì)嘗試在執(zhí)行腳本的同一目錄中打開(kāi)文件。
- 1 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)
0/150
提交
取消