首页 > 解决方案 > htaccess 将 PHP 重写为 PNG,然后 PHP 文件旋转图像数组 - 在 PNG 重写中不起作用

问题描述

我有一个文件: http://example.com/rotating/bg.php

我已经重写为http://example.com/rotating/bg.png使用以下 .htaccess 代码:

RewriteRule \.png$ bg.php [L,NC]

在 bg.php 上,我有一个基本上在刷新时随机加载图像数组的代码,代码如下:

<?php

$imagesDir = 'img/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments


$file_out = $randomImage; // The image to return

if (file_exists($file_out)) {

   $image_info = getimagesize($file_out);

   //Set the content-type header as appropriate
   header('Content-Type: ' . $image_info['mime']);

   //Set the content-length header
   header('Content-Length: ' . filesize($file_out));

   //Write the image bytes to the client
   readfile($file_out);
}
else { // Image file not found

    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");

}?>

我希望 bg.png 的行为方式相同,并且还会重新加载图像,但是它会加载 1 张图像并且无论我刷新多少次都不会更改它。

我接近这个错误吗?任何帮助使它工作将不胜感激!

标签: php.htaccess

解决方案


推荐阅读