【问题标题】:Loading images from a directory into an array with php使用php将目录中的图像加载到数组中
【发布时间】:2015-02-12 13:43:12
【问题描述】:

所以我花了一整天的时间来尝试做一些实际上知道如何在不到 2 分钟内编写 php 的人。令人沮丧,但我通过实践和尝试解决问题来学习。

如果没有得到这个,我会觉得很失败,但是 8 小时和计数(是的,我知道蹩脚)就足够了。

谁能告诉我这个等式有什么问题...

$dir = '../folder';

$images_array = glob($dir.'*.jpg');

$values['options'] = array( '<img src="$images_array"/>');

这可能很明显,但我只需要将 mysite.com/folder 中的图像加载到 $values['options'] 数组中。

如果我只是在其中声明单个图像的路径,则会显示该图像(显然是因为它不依赖于其他任何东西。)

谢谢。

@hellcode

对于您回复下方“评论”中的混乱情况,我们深表歉意。不幸的是我无法让它工作?也许我需要提供更多背景信息。

文件夹中的图像将用作表单中的复选框项目。这是我的原始代码(不工作):

add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
  if($field->id == 187){
    $dir = '../folder';
    $images_array = glob($dir.'*.jpg');
    $values['options'] = array( '<img src="$images_array"/>');
    $values['use_key'] = true;
  }
  return $values;
}

我像这样添加了您的代码:

add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
  if($field->id == 187){
    $dir = '../folder';
    $images_array = glob($dir.'*.jpg');
    $values['options'] = array();
    foreach($images_array as $image) {
      $values['options'][] = '<img src="'.$image.'"/>';
    }
    $values['use_key'] = true;
  }
  return $values;
}

但不幸的是它没有拉入文件:(

【问题讨论】:

    标签: php wordpress image dynamic load


    【解决方案1】:

    试试:

    $dir = '../folder';
    $images_array = glob($dir.'*.jpg');
    $values['options'] = array();
    foreach($images_array as $image) {
      $values['options'][] = '<img src="'.$image.'"/>';
    }
    

    【讨论】:

    • 您不必在 img 标签中添加$dir。在这种情况下,$image 已经包含文件的路径
    • 感谢您的回复。不幸的是我无法让它工作?也许我需要提供更多上下文。文件夹中的图像将用作表单中的复选框项目。这是完整的代码:add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2); function frm_set_checked($values, $field){ if($field-&gt;id == 187){ $dir = '../proofing'; $images_array = glob($dir.'*.jpg'); foreach($images_array as $image) { $values['options'] = array( '&lt;img src="'.$image.'"/&gt;'); $values['use_key'] = true; } } return $values; }
    • @Bevels:添加的代码 sn -p 不足以检测错误。调用代码将如何处理返回的数组 $values 很重要。
    • @Bevels:你在使用 WordPress 吗?然后你应该将标签添加到你的问题中以获得专家。
    • 感谢@Hellcode - 是的,我正在使用 Wordpress。对不起,我什至没有想到要提它!
    【解决方案2】:

    嗯,一个问题可能是glob() 函数使用当前目录,它可以是任何东西,除非你使用chdir() 函数。

    绝对有问题的一件事是您将glob() 的返回值$images_array 用作字符串。因为它是一个行不通的数组。

    这应该是可行的。

    // Allowed image formats (also known as a "whitelist")
    $allowedFormats = ['jpg', 'jpeg', 'gif', 'png'];
    
    // Array for holding any found images
    $foundImages = [];
    
    // Get the real path from the relative path
    $path = realpath('../folder');
    if ($path === false) {
        die('The path does not exist!');
    }
    // Open a folder handle
    $folder = dir($path);
    
    // Read what is in the folder
    while (($item = $folder->read()) !== false) {
    
        // .. is the parent folder, . is the current folder
        if ($item === '..' or $item === '.') {
            continue;
        }
        // Find the last dot in the filename
        // If it was not found then not image file
        $lastDot = strrpos($item, '.');
        if ($lastDot === false) {
            continue;
        }
        // Get the filetype and make sure it is
        // an allowed format
        $filetype = substr($item, $lastDot);
        if ( ! in_array($filetype, $allowedFormats)) {
            continue;
        }
        // Okay, looks like an image!
        $foundImages[] = $item;
    }
    // Close the folder handle
    $folder->close();
    

    【讨论】:

    • 感谢您的回复 Sverri,不幸的是,由于我对 PHP 的了解有限,您的回答离上下文太远了,无法帮助我。
    猜你喜欢
    • 1970-01-01
    • 2012-03-31
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多