使用php将图像从目录加载到数组中
因此,我花了整整一天的时间试图做一些事情,这些事情需要实际上知道如何在不到2分钟的时间内编写php的人来完成。令人沮丧,但我通过实践和努力解决问题来学习 我会因为没有得到这个而觉得自己是个失败者,但8小时和数数(是的,我知道这很糟糕)就足够了 谁能告诉我这个等式有什么问题吗使用php将图像从目录加载到数组中,php,wordpress,image,dynamic,load,Php,Wordpress,Image,Dynamic,Load,因此,我花了整整一天的时间试图做一些事情,这些事情需要实际上知道如何在不到2分钟的时间内编写php的人来完成。令人沮丧,但我通过实践和努力解决问题来学习 我会因为没有得到这个而觉得自己是个失败者,但8小时和数数(是的,我知道这很糟糕)就足够了 谁能告诉我这个等式有什么问题吗 $dir = '../folder'; $images_array = glob($dir.'*.jpg'); $values['options'] = array( '<img src="$images_arra
$dir = '../folder';
$images_array = glob($dir.'*.jpg');
$values['options'] = array( '<img src="$images_array"/>');
$dir='../folder';
$images_array=glob($dir.'*.jpg');
$values['options']=数组(“”);
这可能很明显,但我只需要将mysite.com/folder中的图像加载到$values['options']数组中
如果我只是在中声明了单个图像的路径,那么就会显示该图像(显然,因为它不依赖于任何其他内容)
谢谢
@地狱代码
很抱歉,您的回复下方的“评论”中出现了混乱。不幸的是,我不能让它工作?也许我需要提供更多的背景
文件夹中的图像将用作表单中的复选框项。这是我的原始代码(不工作):
add_filter('frm_setup_new_fields_vars','frm_set_checked',20,2);
函数frm\u设置\u已检查($values,$field){
如果($field->id==187){
$dir='../folder';
$images_array=glob($dir.'*.jpg');
$values['options']=数组(“”);
$values['use_key']=true;
}
返回$value;
}
我添加了如下代码:
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;
}
add_filter('frm_setup_new_fields_vars','frm_set_checked',20,2);
函数frm\u设置\u已检查($values,$field){
如果($field->id==187){
$dir='../folder';
$images_array=glob($dir.'*.jpg');
$values['options']=array();
foreach($images\u数组作为$image){
$values['options'][]='';
}
$values['use_key']=true;
}
返回$value;
}
但不幸的是,它没有把文件拉进来:(试试:
$dir = '../folder';
$images_array = glob($dir.'*.jpg');
$values['options'] = array();
foreach($images_array as $image) {
$values['options'][] = '<img src="'.$image.'"/>';
}
$dir='../folder';
$images_array=glob($dir.'*.jpg');
$values['options']=array();
foreach($images\u数组作为$image){
$values['options'][]='';
}
嗯,一个问题可能是glob()
函数使用当前目录,它可以是任何目录,除非您使用chdir()
函数
有一件事肯定是个问题,那就是您正在使用glob()
的返回值$images\u 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();
您不必在img标记中添加
$dir
。在这种情况下,$image
已经包含了文件的路径。感谢您的响应。很遗憾,我无法使其正常工作?也许我需要提供更多上下文。文件夹中的图像将用作表单中的复选框项。这是完整代码:添加过滤器('frm_setup_new_fields_vars','frm_set_checked',20,2);函数frm_set_checked($values,$field){if($field->id==187){$dir='../proof';$images_array=glob($dir.*.jpg');foreach($images_array作为$image){$values['options']=array('');$values['options']=array('');$values[$value['use_key']=true;}
@Bevels:添加的代码片段不足以检测错误。调用代码如何处理返回的数组$value很重要。@Bevels:你在使用WordPress吗?那么你应该在你的问题中添加标签以获得专家的帮助。谢谢@Hellcode-是的,我在使用WordPress。很抱歉,我甚至没有想到要提到它!我同意感谢您的回复Sverri,不幸的是,由于我对PHP的了解有限,您的回答太离题,无法帮助我。
// 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();