dongxia9519 2013-11-08 17:31
浏览 60
已采纳

在PHP中的glob()中转义空格?

I have the following function in PHP, which is working great except for files with spaces in their names (Good picture.jpg for example). Here it is:

function getphotolist($currentalbum) {
    $photos = glob($currentalbum.'/*.[Jj][Pp][Gg]');
    $albumparts = explode('_', $currentalbum);
    switch (array_key_exists(2,$albumparts)) {
        case false:
            usort($photos,"cmpexiftime");
            break;
        case true:
            usort($photos,"cmpexiftimer");
            break;
    }
    $photolist = "";
    foreach($photos as $photo){ 
        $phototitle = explode('_',basename($photo,".jpg"));
        $title = $phototitle[0];
        $thumb = $currentalbum.'/thumbs/'.basename($photo,".jpg").'_thumb.jpg';
        $exif = read_exif_data_raw("$photo",0);
        $desc = $exif["IFD0"]["ImageDescription"];
        $camera = ucwords(strtolower($exif["IFD0"]["Model"]));
        switch($exif["SubIFD"]["LensInfo"]) {
            case "105.0 mm f/2.8":
                $lens = "AF-S Micro-Nikkor 105mm ƒ/2.8 VR";
                break;
            case "50.0 mm f/1.8":
                if ($camera=="Nikon D700") {
                    $lens = "AF-S Nikkor 50mm ƒ/1.8 G";
                } else {
                    $lens = "AF Nikkor 50mm ƒ/1.8 D";
                }
                break;
            case "18.0-55.0 mm f/3.5-5.6":
                $lens = "AF-S Nikkor 18-55mm ƒ/3.5-5.6 II";
                break;
            default:
                $lens = $exif["SubIFD"]["LensInfo"];
                break;
        }
        $length = str_replace(" ","",$exif["SubIFD"]["FocalLength"]);
        $shutter = str_replace(" ","",str_replace("ec","",$exif["SubIFD"]["ShutterSpeedValue"]));
        $aperture = str_replace("f","ƒ",$exif["SubIFD"]["ApertureValue"]);
        $iso = $exif["SubIFD"]["ISOSpeedRatings"];
        list($width,$height) = getimagesize($photo);
        if ($height==1080 && $width==1920) {
            $photolist .= '<span data-title="'.$title.'" data-thumb="'.$thumb.'" data-desc="'.$desc.'" data-camera="'.$camera.'" data-lens="'.$lens.'" data-length="'.$length.'" data-shutter="'.$shutter.'" data-aperture="'.$aperture.'" data-iso="'.$iso.'" style="background-image:url('.$photo.'); background-size:cover;" class="slide"></span>';
        } elseif ($height >= $width) {                            
            $photolist .= '<span data-title="'.$title.'" data-thumb="'.$thumb.'" data-desc="'.$desc.'" data-camera="'.$camera.'" data-lens="'.$lens.'" data-length="'.$length.'" data-shutter="'.$shutter.'" data-aperture="'.$aperture.'" data-iso="'.$iso.'" style="background-image:url('.$photo.'); background-size:contain;" class="slide"></span>';
        } else {
            $photolist .= '<span data-title="'.$title.'" data-thumb="'.$thumb.'" data-desc="'.$desc.'" data-camera="'.$camera.'" data-lens="'.$lens.'" data-length="'.$length.'" data-shutter="'.$shutter.'" data-aperture="'.$aperture.'" data-iso="'.$iso.'" style="background-image:url('.$photo.'); background-size:contain;" class="slide"></span>';
        }
    }
return $photolist;
}

Can anyone suggest how to fix this to work with files with spaces? Thanks!

  • 写回答

1条回答 默认 最新

  • doulangchao8934 2013-11-08 19:08
    关注

    As explained here, try using this code to escape space in filename when using glob:

    $albumEscaped = str_replace(' ', '\ ', $currentalbum); 
    $photos = glob($albumEscaped .'/*.[Jj][Pp][Gg]');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。