亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法用 scandir 循環遍歷文件夾?我在循環時遇到問題

有沒有辦法用 scandir 循環遍歷文件夾?我在循環時遇到問題

交互式愛情 2023-12-19 10:36:41
首先,我聲明變量來獲取文件夾,并將文件放入數組中。見下文  $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

}

在僅調用文件名的情況下,腳本會嘗試在執行腳本的同一目錄中打開文件。


查看完整回答
反對 回復 2023-12-19
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號