在PHP中的glob()中转义空格? [英] Escape spaces in glob() in PHP?

查看:80
本文介绍了在PHP中的glob()中转义空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中具有以下功能,该功能非常有用,除了名称中带有空格的文件(例如,Good picture.jpg)之外.在这里:

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!

推荐答案

此处,请尝试使用以下代码在使用glob时转义文件名中的空格:

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]');

这篇关于在PHP中的glob()中转义空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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