通过功能从目录列出图像 [英] List images from directory by function

查看:100
本文介绍了通过功能从目录列出图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个功能:

  function getmyimages($ qid){
$ imgdir ='modules /项目/ uploaded_project_images /。 $ qid。''; //存储图像的目录
$ allowed_types = array('png','jpg','jpeg','gif'); //要显示的文件类型列表

$ dimg = opendir($ imgdir);
while($ imgfile = readdir($ dimg))
{
if(in_array(strtolower(substr($ imgfile,-3)),$ allowed_types))
{
$ a_img [] = $ imgfile;
sort($ a_img);
重置($ a_img);
}
}

$ totimg = count($ a_img); //总图像数

($ x = 0; $ x <$ totimg; $ x ++)
{
$ size = getimagesize($ imgdir。 。$ a_img [$ X]);

// do whatever
$ halfwidth = ceil($ size [0] / 2);
$ halfheight = ceil($ size [1] / 2);
$ mytest ='name:'$ a_img [$ x]。'width:'。$ size [0]。'height:'。$ size [1]。'< br />一个href ='。$ imgdir。'/'。$ a_img [$ x]。'>'。 $ a_img [$ X]。 < / A>;

}

return $ mytest;
}

我将这个函数称为$ code>而 row as:

  $ sql_select = $ db-> sql_query('SELECT * from'。$ prefix .'_ projects WHERE topic = \''。$ cid.'\''); 
OpenTable();
while($ row2 = $ db-> sql_fetchrow($ sql_select)){
$ qid = $ row2 ['qid'];

$ project_query = $ db-> sql_query('SELECT p.uid,p.uname,p.subject,p.story,p.storyext,p.date,p.topic,p。 pdate,p.materials,p.bidoptions,p.projectduration,pd.id_duration,pm.material_id,pbo.bidid,pc.cid FROM'。$ prefix。'_projects p,'。$ prefix。'_projects_duration pd,'。 $ prefix。'_project_materials pm,'。$ prefix。'_project_bid_options pbo,'。$ prefix。'_project_categories pc WHERE p.topic = \''。$ cid.'\'和p.qid = \'' $ qid.'\'和p.bidoptions = pbo.bidid和p.materials = pm.material_id和p.projectduration = pd.id_duration');

while($ project_row = $ db-> sql_fetchrow($ project_query)){
// $ qid = $ project_row ['qid'];
$ uid = $ project_row ['uid'];
$ uname = $ project_row ['uname'];
$ subject = $ project_row ['subject'];
$ story = $ project_row ['story'];
$ storyext = $ project_row ['storyext'];
$ date = $ project_row ['date'];
$ topic = $ project_row ['topic'];
$ pdate = $ project_row ['pdate'];
$ materials = $ project_row ['materials'];
$ bidoptions = $ project_row ['bidoptions'];
$ projectduration = $ project_row ['projectduration'];

//获取主题名称
$ topic_query = $ db-> sql_query('SELECT cid,title from'。$ prefix。'_ project_categories WHERE cid = \'。$ cid.'\ '');
while($ topic_row = $ db-> sql_fetchrow($ topic_query)){
$ topic_id = $ topic_row ['cid'];
$ topic_title = $ topic_row ['title'];
}
//获取材料文本
$ material_query = $ db-> sql_query('SELECT material_id,material_name from'。$ prefix。'_ project_materials WHERE material_id = \''。 $ materials.'\ '');
while($ material_row = $ db-> sql_fetchrow($ material_query)){
$ material_id = $ material_row ['material_id'];
$ material_name = $ material_row ['material_name'];
}
//获取出价方法
$ bid_query = $ db-> sql_query('SELECT bidid,bidname from'。$ prefix。'_ project_bid_options WHERE bidid = \''。 $ bidoptions.'\ '');
while($ bid_row = $ db-> sql_fetchrow($ bid_query)){
$ bidid = $ bid_row ['bidid'];
$ bidname = $ bid_row ['bidname'];
}
//获取项目持续时间
$ duration_query = $ db-> sql_query('SELECT id_duration,duration_value,duration_alias from'。$ prefix。'_ projects_duration WHERE id_duration = \' 。$ projectduration.'\ '');
while($ duration_row = $ db-> sql_fetchrow($ duration_query)){
$ id_duration = $ duration_row ['id_duration'];
$ duration_value = $ duration_row ['duration_value'];
$ duration_alias = $ duration_row ['duration_alias'];
}


}


echo'< br />< b> id< / b> --- >'。$ qid。 '< br />< b> uid< / b> --->'。$ uid。 '< br />< b> username< / b> --->'。$ uname。 <峰; br />< b取代;受试对象; / B个--->。$主题。 <峰; br />< b取代; story1< / B个--->。$故事。 <峰; br />< b取代; story2< / B个--->。$ storyext。 <峰; br />< b取代;踵< / B个--->。$日期。 '< br />< b> category< / b> --->'。$ topic_title。 '< br />< b> project start< / b> --->'。$ pdate。 <峰; br />< b取代;材料< / B个--->。$ material_name。 '< br />< b>出价方法< / b> --->'。$ bidname。 '< br />< b>项目持续时间< / b> --->'。$ duration_alias。'< br />< br />< br />< b> image url< / b> --->'。getmyimages($ qid)。'< br />< br />';

}


CloseTable();

结果仅输出目录中的最后文件。
如果我做一个 echo 而不是一个 return $ mytest; ,它读取整个目录但是废墟

解决方案

您总是覆盖 $ mytest 在你的循环中:

  for($ x = 0; $ x< $ totimg; $ x ++)
{
$ size = getimagesize($ imgdir。'/'。$ a_img [$ x]);
// do whatever
$ halfwidth = ceil($ size [0] / 2);
$ halfheight = ceil($ size [1] / 2);
$ mytest ='name:'$ a_img [$ x]。'width:'。$ size [0]。'height:'。$ size [1]。'< br />一个href ='。$ imgdir。'/'。$ a_img [$ x]。'>'。 $ a_img [$ X]。 < / A>;
}
return $ mytest;

循环后, $ mytest 包含循环中生成的最后一个值。你可能想要返回一个数组:

  $ mytest = array(); 

($ x = 0; $ x <$ totimg; $ x ++)
{
$ size = getimagesize($ imgdir。'/'$ a_img [$ X]);
// do whatever
$ halfwidth = ceil($ size [0] / 2);
$ halfheight = ceil($ size [1] / 2);
$ mytest [] ='name:'$ a_img [$ x]。'width:'。$ size [0]。'height:'。$ size [1]。'< br /> < a href ='。$ imgdir。'/'。$ a_img [$ x]。'>'。 $ a_img [$ X]。 < / A>;
}

return $ mytest;

但是,当然,您还必须更改其他函数才能处理返回的数组。 / p>

I'm using this function:

function getmyimages($qid){
$imgdir = 'modules/Projects/uploaded_project_images/'. $qid .''; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show

$dimg = opendir($imgdir);
while($imgfile = readdir($dimg))
{
 if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
 {
  $a_img[] = $imgfile;
  sort($a_img);
  reset ($a_img);
 } 
}

$totimg = count($a_img); // total image number

for($x=0; $x < $totimg; $x++)
{
 $size = getimagesize($imgdir.'/'.$a_img[$x]);

 // do whatever
 $halfwidth = ceil($size[0]/2);
 $halfheight = ceil($size[1]/2);
  $mytest =  'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br /><a href="'. $imgdir .'/'.$a_img[$x].'">'. $a_img[$x]. '</a>';

 }

 return $mytest;
}

And I call this function between a while row as:

$sql_select = $db->sql_query('SELECT * from '.$prefix.'_projects WHERE topic=\''.$cid.'\'');
OpenTable();
while ($row2 = $db->sql_fetchrow($sql_select)){
$qid = $row2['qid'];

                $project_query = $db->sql_query('SELECT p.uid, p.uname, p.subject, p.story, p.storyext, p.date, p.topic, p.pdate, p.materials, p.bidoptions, p.projectduration, pd.id_duration, pm.material_id, pbo.bidid, pc.cid FROM ' . $prefix . '_projects p,  ' . $prefix . '_projects_duration pd, ' . $prefix . '_project_materials pm, ' . $prefix . '_project_bid_options pbo, ' . $prefix . '_project_categories pc WHERE p.topic=\''.$cid.'\' and p.qid=\''.$qid.'\' and p.bidoptions=pbo.bidid and p.materials=pm.material_id and p.projectduration=pd.id_duration');

                while ($project_row = $db->sql_fetchrow($project_query)) {
                //$qid = $project_row['qid'];
                $uid = $project_row['uid'];
                $uname = $project_row['uname'];
                $subject = $project_row['subject'];
                $story = $project_row['story'];
                $storyext = $project_row['storyext'];
                $date = $project_row['date'];
                $topic = $project_row['topic'];
                $pdate = $project_row['pdate'];
                $materials = $project_row['materials'];
                $bidoptions = $project_row['bidoptions'];
                $projectduration = $project_row['projectduration'];

                //Get the topic name
                $topic_query = $db->sql_query('SELECT cid,title from '.$prefix.'_project_categories WHERE cid =\''.$cid.'\'');
                while ($topic_row = $db->sql_fetchrow($topic_query)) {
                $topic_id = $topic_row['cid'];
                $topic_title = $topic_row['title'];
                }
                //Get the material text
                $material_query = $db->sql_query('SELECT material_id,material_name from '.$prefix.'_project_materials WHERE material_id =\''.$materials.'\'');
                while ($material_row = $db->sql_fetchrow($material_query)) {
                $material_id = $material_row['material_id'];
                $material_name = $material_row['material_name'];
                }
                //Get the bid methode
                $bid_query = $db->sql_query('SELECT bidid,bidname from '.$prefix.'_project_bid_options WHERE bidid =\''.$bidoptions.'\'');
                while ($bid_row = $db->sql_fetchrow($bid_query)) {
                $bidid = $bid_row['bidid'];
                $bidname = $bid_row['bidname'];
                }
                //Get the project duration
                $duration_query = $db->sql_query('SELECT id_duration,duration_value,duration_alias from '.$prefix.'_projects_duration WHERE id_duration =\''.$projectduration.'\'');
                while ($duration_row = $db->sql_fetchrow($duration_query)) {
                $id_duration = $duration_row['id_duration'];
                $duration_value = $duration_row['duration_value'];
                $duration_alias = $duration_row['duration_alias'];
                }


                }


                echo '<br/><b>id</b>--->' .$qid. '<br/><b>uid</b>--->' .$uid. '<br/><b>username</b>--->' .$uname. '<br/><b>subject</b>--->'.$subject. '<br/><b>story1</b>--->'.$story. '<br/><b>story2</b>--->'.$storyext. '<br/><b>postdate</b>--->'.$date. '<br/><b>categorie</b>--->'.$topic_title . '<br/><b>project start</b>--->'.$pdate. '<br/><b>materials</b>--->'.$material_name. '<br/><b>bid methode</b>--->'.$bidname. '<br/><b>project duration</b>--->'.$duration_alias.'<br /><br /><br/><b>image url</b>--->'.getmyimages($qid).'<br /><br />';

}


CloseTable();

The result outputs only the "last" file from the directories. If I do an echo instead of a return $mytest;, it reads the whole directory but ruins the output.

解决方案

You are always overwriting $mytest in your loop:

for($x=0; $x < $totimg; $x++)
{
   $size = getimagesize($imgdir.'/'.$a_img[$x]);
   // do whatever
   $halfwidth = ceil($size[0]/2);
   $halfheight = ceil($size[1]/2);
   $mytest =  'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br /><a href="'. $imgdir .'/'.$a_img[$x].'">'. $a_img[$x]. '</a>'; 
}
return $mytest;

So after the loop, $mytest contains the last value that was generated in the loop. You might want to return an array instead:

$mytest = array();

for($x=0; $x < $totimg; $x++)
{
   $size = getimagesize($imgdir.'/'.$a_img[$x]);
   // do whatever
   $halfwidth = ceil($size[0]/2);
   $halfheight = ceil($size[1]/2);
   $mytest[] =  'name: '.$a_img[$x].' width: '.$size[0].' height: '.$size[1].'<br /><a href="'. $imgdir .'/'.$a_img[$x].'">'. $a_img[$x]. '</a>'; 
}

return $mytest;

But of course you also have to change your other function in order to deal with the returned array.

这篇关于通过功能从目录列出图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆