opendir(path,context)

本文详细介绍了使用PHP操作目录的基本语法和用法,通过实际代码示例展示了如何使用opendir()、readdir()和closedir()函数来遍历目录内容,并讨论了如何处理可能的错误情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

path必需。规定要打开的目录路径。
context可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项。

例子 1

<?php
//打开 images 目录
$dir = opendir("images");

//列出 images 目录中的文件
while (($file = readdir($dir)) !== false)
  {
  echo "filename: " . $file . "<br />";
  }
  closedir($dir);
?> 

输出:

filename: .
filename: ..
filename: cat.gif
filename: dog.gif
filename: food
filename: horse.gif

如果 opendir() 的输出失败,本例会隐藏错误:
$dir = @ opendir("images");

转载于:https://www.cnblogs.com/bafeiyu/archive/2012/12/27/2835611.html

#define MAX_PATH_LEN 1024 #define MAX_MATCHES 1024 #define DEBUG_PRINT(fmt, ...) \ do { fprintf(stderr, "[DEBUG] %s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); } while (0) typedef enum { MODE_NEW, MODE_CYCLE } completion_mode; typedef struct { char current_path[MAX_PATH_LEN]; char prefix[MAX_PATH_LEN]; char *matches[MAX_MATCHES]; int match_count; int current_index; char *last_returned; char last_attempt[MAX_PATH_LEN]; int is_explicit_dir; } completion_state; static completion_state comp_state; void reset_completion_state() { DEBUG_PRINT("Resetting state (had %d matches)\n", comp_state.match_count); for (int i = 0; i < comp_state.match_count; i++) { XFREE(MTYPE_TMP, comp_state.matches[i]); } XFREE(MTYPE_TMP, comp_state.last_returned); memset(&comp_state, 0, sizeof(comp_state)); } void parse_input_text(const char *text) { DEBUG_PRINT("Parsing input: '%s'\n", text); strncpy(comp_state.last_attempt, text, MAX_PATH_LEN-1); strncpy(comp_state.current_path, text, MAX_PATH_LEN-1); char *last_slash = strrchr(comp_state.current_path, '/'); if (last_slash) { strncpy(comp_state.prefix, last_slash+1, MAX_PATH_LEN-1); *(last_slash+1) = '\0'; comp_state.is_explicit_dir = (text[strlen(text)-1] == '/'); } else { strncpy(comp_state.prefix, comp_state.current_path, MAX_PATH_LEN-1); comp_state.current_path[0] = '\0'; comp_state.is_explicit_dir = 0; } DEBUG_PRINT("Parsed: path='%s' prefix='%s' explicit_dir=%d\n", comp_state.current_path, comp_state.prefix, comp_state.is_explicit_dir); } void generate_matches_for_current_context() { DEBUG_PRINT("Generating matches for context: path='%s' prefix='%s'\n", comp_state.current_path, comp_state.prefix); DIR *dir = opendir(comp_state.current_path[0] ? comp_state.current_path : "."); if (!dir) { DEBUG_PRINT("Failed to open dir\n"); return; } struct dirent *entry; while ((entry = readdir(dir)) != NULL && comp_state.match_count < MAX_MATCHES) { if (entry->d_name[0] == '.') continue; if (!comp_state.is_explicit_dir && comp_state.current_path[0] != '\0') { DEBUG_PRINT("Skipping '%s' (not explicit dir mode)\n", entry->d_name); continue; } if (comp_state.prefix[0] && strncmp(entry->d_name, comp_state.prefix, strlen(comp_state.prefix)) != 0) { DEBUG_PRINT("Skipping '%s' (prefix mismatch)\n", entry->d_name); continue; } char full_path[MAX_PATH_LEN]; snprintf(full_path, sizeof(full_path), "%s%s", comp_state.current_path, entry->d_name); struct stat st; if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) { snprintf(full_path, sizeof(full_path), "%s%s/", comp_state.current_path, entry->d_name); DEBUG_PRINT("Found dir: %s\n", full_path); } else { DEBUG_PRINT("Found file: %s\n", full_path); } comp_state.matches[comp_state.match_count] = XSTRDUP(MTYPE_TMP, full_path); comp_state.match_count++; } closedir(dir); DEBUG_PRINT("Total matches: %d\n", comp_state.match_count); } char *filename_completion_function(const char *text, int state) { DEBUG_PRINT("\n=== CALL: text='%s' state=%d ===\n", text, state); completion_mode mode = (state == 0) ? MODE_NEW : MODE_CYCLE; DEBUG_PRINT("Mode %s\n", mode == MODE_NEW ? "NEW" : "CYCLE"); if (mode == MODE_NEW) { reset_completion_state(); parse_input_text(text); generate_matches_for_current_context(); comp_state.current_index = 0; } else { comp_state.current_index = (comp_state.current_index + 1) % comp_state.match_count; DEBUG_PRINT("Cycling to index %d\n", comp_state.current_index); } char *ret = NULL; if (comp_state.current_index < comp_state.match_count) { ret = XSTRDUP(MTYPE_TMP, comp_state.matches[comp_state.current_index]); DEBUG_PRINT("Returning: %s\n", ret); XFREE(MTYPE_TMP, comp_state.last_returned); comp_state.last_returned = XSTRDUP(MTYPE_TMP, ret); } else { DEBUG_PRINT("No matches to return\n"); } return ret; } 我现在改成这样了,看看还有什么问题?
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值