wen0750
V2EX  ›  PHP

PHP scandir 上的问题

  •  
  •   wen0750 · Feb 3, 2020 via Android · 3700 views
    This topic created in 2295 days ago, the information mentioned may be changed or developed.

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

    $file

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

    output :

    Sample-Apply.png

    Sample-Banana.png

    Sample-Cherry.png

    7 replies    2020-02-04 22:52:15 +08:00
    wen0750
        1
    wen0750  
    OP
       Feb 3, 2020 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  
       Feb 3, 2020 via iPhone
    字符串查找
    wen0750
        3
    wen0750  
    OP
       Feb 3, 2020 via Android
    @jugelizi 想問一下大致上要怎麼做?
    wen0750
        4
    wen0750  
    OP
       Feb 3, 2020 via Android
    @jugelizi 想问一下大致上要怎么做?
    garlics
        5
    garlics  
       Feb 3, 2020   ❤️ 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
       Feb 3, 2020 via Android
    @garlics 谢谢大哥你的帮忙。
    wen0750
        7
    wen0750  
    OP
       Feb 4, 2020
    <?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);
    ?>
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   871 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:23 · PVG 05:23 · LAX 14:23 · JFK 17:23
    ♥ Do have faith in what you're doing.