如何在PHP中从目录中选择一个图像?
我只知道图像名,不知道图像扩展名(jpg、gif等)(代码自动选择)。目录名“上载” 我在上传文件夹中有许多图像,如(Image1、Image2、Image3),但我只想选择一个图像如何在PHP中从目录中选择一个图像?,php,Php,我只知道图像名,不知道图像扩展名(jpg、gif等)(代码自动选择)。目录名“上载” 我在上传文件夹中有许多图像,如(Image1、Image2、Image3),但我只想选择一个图像(Image1.(jpg)(gif)(png))。请给我PHP代码选择图像从目录与任何扩展名 $img_detail = '11_detail'; // i wanna select this image $dir = 'upload/advertisement/'; $
(Image1.(jpg)(gif)(png))
。请给我PHP代码选择图像从目录与任何扩展名
$img_detail = '11_detail'; // i wanna select this image
$dir = 'upload/advertisement/';
$files = scandir($dir);
foreach($files as $file){
echo $file.'<br>';
}
// Code showing result like thi
// 11.swf
// 13.gif
// 14.gif
// 15.gif
// 16.jpg
// 16_detail.gif // i wanna select this image
// index.php
$img_detail='11_detail';//我想选择这个图像
$dir='upload/advision/';
$files=scandir($dir);
foreach($files作为$file){
回显$file.“
”;
}
//显示类似于thi的结果的代码
//11.swf
//13.gif
//14.gif
//15.gif
//16.jpg
//16_detail.gif//我要选择此图像
//index.php
所有图像扩展不一样享受吧
$ad_id = '11';
$addata = ad_data($conn, $ad_id, 'ad_img'); // Function
$addetail = $ad_id.'_detail'; // this is my file name
$dir = 'upload/advertisement/';
$files = scandir($dir);
foreach($files as $file){
$file = explode('.', $file);
$currentname = current($file); // i have select current name
$ext = end($file); // for final result
// echo $firstname. '-'.$ext.'<br>'; // test
if($currentname == $addetail){ // matching image name with required name
$real_file = $currentname.'.'.$ext; // after matching use real extension
}
}
if(empty($real_file)){ // if file not found
echo $ad_id.'.'.$addata['ad_img'];
}else{ // enjoy
echo $real_file;
}
$ad_id='11';
$addata=ad_数据($conn,$ad_id,'ad_img');//作用
$addetail=$ad_id.“u detail”;//这是我的文件名
$dir='upload/advision/';
$files=scandir($dir);
foreach($files作为$file){
$file=分解('.',$file);
$currentname=current($file);//我已经选择了当前名称
$ext=end($file);//获取最终结果
//echo$firstname.'-'.$ext.
';//测试
如果($currentname==$addetail){//将图像名称与所需名称匹配
$real_file=$currentname...$ext;//匹配后使用实扩展名
}
}
if(空($real_file)){//if未找到文件
echo$ad_id.'.$addata['ad_img'];
}否则{//享受
echo$real_文件;
}
当您扫描目录并回显找到的每个文件时,很明显您读取了浏览器中的每个图像。只有在Regex匹配为true时,才必须通过Regex和echo检查每个文件。有关详细信息,请参阅。。。或者只需使用glob
。。。