V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wen0750
V2EX  ›  PHP

PHP scandir 上的问题

  •  
  •   wen0750 · 2020-02-03 02:26:35 +08:00 via Android · 2938 次点击
    这是一个创建于 1550 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想请教一下各位会 php 的大大 /大佬们,小弟想利用 php scandir 的功能,在网页上显示某个资料夹的所有文件,但不知如何才可以仅在 output 时把文件的某个字词 删去,例如把以下"sample"删去。

    $file

    "; $i++; } } closedir($dh); ?>

    output :

    Sample-Apply.png

    Sample-Banana.png

    Sample-Cherry.png

    7 条回复    2020-02-04 22:52:15 +08:00
    wen0750
        1
    wen0750  
    OP
       2020-02-03 02:29:01 +08:00 via Android
    <?php
    $path = ".";
    $dh = opendir($path);
    $i=1;
    while (($file = readdir($dh)) !== false) {
    if($file !="filelist.php" && $file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {
    echo "<a href='$path/$file'>$file</a><br /><br />";
    $i++;
    }
    }
    closedir($dh);
    ?>
    jugelizi
        2
    jugelizi  
       2020-02-03 10:47:09 +08:00 via iPhone
    字符串查找
    wen0750
        3
    wen0750  
    OP
       2020-02-03 13:05:30 +08:00 via Android
    @jugelizi 想問一下大致上要怎麼做?
    wen0750
        4
    wen0750  
    OP
       2020-02-03 13:06:10 +08:00 via Android
    @jugelizi 想问一下大致上要怎么做?
    garlics
        5
    garlics  
       2020-02-03 13:50:30 +08:00   ❤️ 1
    ```
    <?php
    $path = ".";
    $dh = opendir($path);
    $i=1;
    while (($file = readdir($dh)) !== false) {
    if($file !="filelist.php" && $file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {
    $file = str_replace("Sample-","",$file);
    echo "<a href='$path/$file'>$file</a><br /><br />";
    $i++;
    }
    }
    closedir($dh);
    ?>
    ```
    wen0750
        6
    wen0750  
    OP
       2020-02-03 19:38:50 +08:00 via Android
    @garlics 谢谢大哥你的帮忙。
    wen0750
        7
    wen0750  
    OP
       2020-02-04 22:52:15 +08:00
    <?php
    $path = ".";
    $dh = opendir($path);
    $i=1;
    while (($file = readdir($dh)) !== false) {
    if($file !="filelist.php" && $file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {
    $name = str_replace("Sample-","",$file);
    echo "<a href='$path/$file'>$name</a><br /><br />";
    $i++;
    }
    }
    closedir($dh);
    ?>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2071 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:47 · PVG 18:47 · LAX 03:47 · JFK 06:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.