按功能列出目录中的图像

发布于 2024-08-27 06:27:45 字数 5018 浏览 0 评论 0原文

我正在使用这个函数:

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;
}

我在 while 行之间调用这个函数:

$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();

结果仅输出目录中的“最后一个”文件。 如果我执行 echo 而不是 return $mytest;,它会读取整个目录,但会破坏输出。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2024-09-03 06:27:45

您总是在循环中覆盖 $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 /><a href="'. $imgdir .'/'.$a_img[$x].'">'. $a_img[$x]. '</a>'; 
}
return $mytest;

因此在循环之后,$mytest 包含循环中生成的最后一个值。您可能想返回一个数组:

$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;

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文