【问题标题】:Check if directory is empty, if not then show image inside, if yes then show a default image检查目录是否为空,如果不是则显示内部图像,如果是则显示默认图像
【发布时间】:2016-09-14 09:43:40
【问题描述】:

我有一点问题。我在我的系统上放置了一张个人资料图片,其中图片存储在一个文件中,我从数据库中调用它。我想要发生的是这样的:

对于尚未上传照片的人,我希望系统打开某个目录作为默认头像。所以,我的代码是这样的:如果有上传的图片,它会检查文件,如果没有,它会打开一个特定的目录。

<?php
  $profquery = "select User_ID from tusers where vName ='".$_SESSION['SESS_vName_JO'] ."' ";
  $search = mysql_query($profquery);

  $row=mysql_fetch_assoc($search);
  $q=$row['User_ID'];


  $dir= "../../profpic/" . $q . "/";
  $profimge=(count(glob($dir)) === 0) ? 'Empty' : 'Not empty';
    if(profimge=="Empty"){
  $pimge  = opendir($profimge);
  $files = array();
  while ($pfiles[] = readdir($pimge));
  natsort($pfiles);
  closedir($pimge); 
  foreach ($pfiles as $pfile)
    { 
      if ($pfile == "." || $pfile == ".." || $pfile == "thumbs.db" || $pfile == "Thumbs.db" || $pfile == "")
      {
        //skip
      }else
      {
        $profImage = "../../profpic/" . $profpic . "/" . $pfile;
        $pathofimage = "../../profpic/" . $profpic . "/";
      }
    } 
  }else if($profimge=="Not empty"){
    $folder = 0;
    $default = "../../profpic/". $folder. "/";
    $pimge  = opendir($default);
    $files = array();
    while ($pfiles[] = readdir($pimge));
    natsort($pfiles);
    closedir($pimge); 
    foreach ($pfiles as $pfile)
      { 
        if ($pfile == "." || $pfile == ".." || $pfile == "thumbs.db" || $pfile == "Thumbs.db" || $pfile == "")
        {
          //skip
        }else
        {
          $profImage = "../../profpic/" . $profpic . "/" . $pfile;
          $pathofimage = "../../profpic/" . $profpic . "/";
        }
      } 
    }
?>

我的第一个代码是这样的,它也不起作用。

<?php


$profquery = "select User_ID from tusers where vName ='".$_SESSION['SESS_vName_JO'] ."' ";
$search = mysql_query($profquery);

$row=mysql_fetch_assoc($search);
$profpic=$row['User_ID'];

$cntImage=0;
$cStatus="";
$FName=$profpic;

$profimge= "../../profpic/" . $profpic . "/";
if(file_exists($profimge)){
$pimge  = opendir($profimge);
$files = array();
while ($pfiles[] = readdir($pimge));
natsort($pfiles);
closedir($pimge); 
  foreach ($pfiles as $pfile)
    { 
      if ($pfile == "." || $pfile == ".." || $pfile == "thumbs.db" || $pfile == "Thumbs.db" || $pfile == "")
      {
        //skip
      }else
      {
        $profImage = "../../profpic/" . $profpic . "/" . $pfile;
        $pathofimage = "../../profpic/" . $profpic . "/";
      }
  } 
}else{
  $folder = 0;
  $default = "../../profpic/". $folder. "/";
  $pimge  = opendir($default);
  $files = array();
  while ($pfiles[] = readdir($pimge));
  natsort($pfiles);
  closedir($pimge); 
  foreach ($pfiles as $pfile)
    { 
      if ($pfile == "." || $pfile == ".." || $pfile == "thumbs.db" || $pfile == "Thumbs.db" || $pfile == "")
      {
        //skip
      }else
      {
        $profImage = "../../profpic/" . $profpic . "/" . $pfile;
        $pathofimage = "../../profpic/" . $profpic . "/";
      }
    } 

}
?>

【问题讨论】:

标签: php html mysql css


【解决方案1】:

检查目录是否为空函数

public static function isEmptyDir($dir){ 
     return (($files = @scandir($dir)) && count($files) <= 2); 
}

【讨论】:

    猜你喜欢
    • 2013-05-27
    • 2012-07-23
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多