简体   繁体   English

如何判断何时获取新图像?

[英]How to tell when to fetch new image?

I have a copy of 350+ images on my sever, when a person tries to view one, if it is more than 5 minutes old, I want my sever to check with another website that I am mirroring data from(they insist on me mirroring instead of hotlinking) and get the newest copy. 我的服务器上有350多张图片的副本,当一个人试图查看一个,如果它超过5分钟,我希望我的服务器检查另一个网站,我正在镜像数据(他们坚持我镜像)而不是hotlinking)并获得最新的副本。 Any thoughts on how to do this? 有关如何做到这一点的任何想法?

I can do a cron script and get all of the images, but there are problems doing that.(My host limits me to once every 15 minutes, I would have to get a lot of images that my users may or may not actually view.) 我可以做一个cron脚本并获取所有图像,但是这样做有问题。(我的主机限制我每15分钟一次,我必须得到很多我的用户可能或可能不会查看的图像。 )

I am thinking there should be a way to do this in PHP, but I have no idea where I would start. 我想在PHP中应该有办法做到这一点,但我不知道我会从哪里开始。

You could serve the images via a php script that allows you to do the necessary checks before displaying the image. 您可以通过php脚本提供图像,允许您在显示图像之前进行必要的检查。

<img src="/index.php/image-name.jpg">

Below is one option for the checks 以下是支票的一个选项

// get the image name from the uri
$image = explode("/", $_SERVER['REQUEST_URI'])[2];
// check if the image exists
if (is_file($image)) {
    // get the file age
    $age = filemtime($image);
    if ($age < time() - (60*5)) { // 5 mins old
        // file too old so check for new one
            // do your check and serve the appropriate image
    }
    else
    {
        // get the image and serve it to the user
        $fp = fopen($image, 'rb');
        // send the right headers
        header("Content-Type: image/jpg");
        header("Content-Length: " . filesize($image));
        // dump the picture and stop the script
        fpassthru($fp);
        exit();
    }
}
else
{
    // handle error
}

You can apply ajax at your project. 您可以在项目中应用ajax。 call your server at every 5 minutes using ajax and refresh your content. 使用ajax每5分钟呼叫您的服务器并刷新您的内容。 In short; 简而言之; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. AJAX是关于在后台加载数据并将其显示在网页上,而无需重新加载整个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM