php代码|获取对应文件夹内的图片,并显示(php获取当前文件夹)

时间:2021-09-03 14:03:36   阅读:1022

就是别人常说的api,有两种方法生成:

第一种.

php代码|获取对应文件夹内的图片,并显示(php获取当前文件夹)

./28/ 28是文件夹呢,修改为你对应的文件夹就行。

<?php
header('Content-type: image/jpg');
$img_array = glob("./28/*.{gif,jpg,png}",GLOB_BRACE);
$img = array_rand($img_array);
$image = file_get_contents($img_array[$img]);
echo $image;
?>



第二种.

请留意 img.txt文件,文件内存放一些图片链接,下面代码会从文件内自动获取链接


<?php
//存有image链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
    die('文件不存在');
}
 
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
    $line=trim(fgets($fs));
    if($line!=''){
        array_push($pics, $line);
    }
}
 
//从数组随机获取链接
$pic = $pics[array_rand($pics)];
 
//返回指定格式
$type=$_GET['type'];
switch($type){
 
//JSON返回
case 'json':
    header('Content-type:text/json');
    die(json_encode(['pic'=>$pic]));
 
default:
    die(header("Location: $pic"));
}
?>


上一篇:完整代码|樱花飘落(樱花飘落的代码)

下一篇:代码|获取当前网站链接代码(获取当前网址代码)

网友评论