【问题标题】:Glob function output all filesglob函数输出所有文件
【发布时间】:2014-02-21 08:49:47
【问题描述】:

您好,我正在使用此代码从名为 images 的文件中输出图像

<?php
//Path to folder which contains images
$dirname = $_POST['dir'];

//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."*");

//Display image using foreach loop
foreach($images as $image) {

//print the image to browser with anchor tag (Use if you want really :) )
echo '<p><img style="height:176px; width:221px; float:right;" src="'.$image.'" /></p>';
}
?>

我把它命名为image.php,我用这个表单让用户选择目录

<form method="POST" action="image.php">
<input type="text" name="dir" placeholder="choose directory" />
<input type="submit" value="choose" />
</form>

当我运行代码时,它会输出文件夹中的所有文件把它不输出图像是什么错误

【问题讨论】:

标签: php


【解决方案1】:

那是非常危险的。任何人都可以键入他们想要的任何文件夹并立即查看(如果是 PHP 文件,则执行)该目录中的所有文件。

请注意您的代码中关于“只需要 JPEG 或 PNG”的注释,您可以这样做:

$images = glob($dirname."*.{png,jpg,jpeg,gif}",GLOB_BRACE);

这将只允许这些类型的图像文件。

您还应该知道glob 不是递归的。为此,您必须手动递归目录。

【讨论】:

    【解决方案2】:

    或者这个,如果你不想过滤扩展:

    //Path to folder which contains images
    $dirname = $_POST['dir'];
    
    //Use glob function to get the files
    //Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
    //below line and commnent $images variable currently in use
    $images = glob($dirname."/"."*");
    
    //Display image using foreach loop
    foreach($images as $image) {
    
    //print the image to browser with anchor tag (Use if you want really :) )
    echo '<p><img style="height:176px; width:221px; float:right;" src="'.$image.'" /></p>';
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 2015-01-06
      • 2021-12-04
      • 2014-06-26
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      相关资源
      最近更新 更多