Home > php教程 > PHP源码 > PHP 随机显示某张图片

PHP 随机显示某张图片

PHP中文网
Release: 2016-05-25 17:14:50
Original
1462 people have browsed it

随机显示指定图像文件夹下的所有图片

<?
# Init Array
$files = array();
# Get Folder
if($_GET[&#39;folder&#39;]) {
    $folder = $_GET[&#39;folder&#39;];
} else {
    # Set Default Folder
    $folder = &#39;/img/&#39;;
}
# Set Full Path
$path = $_SERVER[&#39;DOCUMENT_ROOT&#39;] . &#39;/&#39; . $folder;
# Open Directory
if($handle = opendir($path)) {
    # Loop Through Directory
    while(false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") { 
            if(substr($file, -3) == &#39;gif&#39; || substr($file, -3) == &#39;png&#39; || substr($file, -3) == &#39;jpg&#39; || substr($file, -4) == &#39;jpeg&#39;) $files[count($files)] = $file;
                }
    }
}
# Close Handle
closedir($handle); 
# Init Random
$rand = rand(0, count($files)-1);
# Check Header Type
# GIF
if(substr($files[$random], -3) == &#39;gif&#39;) header("Content-type: image/gif");
# JPEG
elseif(substr($files[$random], -3) == &#39;jpg&#39;) header("Content-type: image/jpeg");
elseif(substr($files[$random], -4) == &#39;jpeg&#39;) header("Content-type: image/jpeg");
# PNG
elseif(substr($files[$random],-3) == &#39;png&#39;) header("Content-type: image/png");
# Read File
readfile($path . &#39;/&#39; . $files[$random]);
?>
Copy after login

                   

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template