Asked  7 Months ago    Answers:  5   Viewed   34 times

I'm totally new to how to cache images.

I output all images in a gallery with PHP, and want the images already shown, to be cached by the browser, so the PHP script don't have to output the same image again. All I want is the images to show up faster.

When calling an image I do like this:

<img src="showImage.php?id=601">

and the showImage.php-file does:

$id = (int) $_GET['id'];
$resultat = mysql_query("
    SELECT filename, id
    FROM Media 
    WHERE id = $id
$data = mysql_fetch_assoc($resultat);


//Only if the user are logged in
    header("Content-Type: image/jpeg");

    //$data['filename'] can be = dsSGKLMsgKkD3325J.jpg



If you are using php to check if the user is logged in before outputting the message, then you don't want the browser to cache the image.

The entire point of caching is to call the server once and then never call it again. If the browser caches the image, it won't call the server and your script won't run. Instead, the browser will pull your image from cache and display it, even if the user is no longer logged in. This could potentially be a very big security hole.

Wednesday, March 31, 2021
answered 7 Months ago

I wrote this file cache function which basically just replaces file_get_contents. You can specify the amount of time the cache should last for in $offset or completely override the cache with $override. If you don't want to use /tmp/, just change that directory to something you can read/write to.

function cache_get_contents($url, $offset = 600, $override = false) {
    $file = '/tmp/file_cache_' . md5($url);
    if (!$override && file_exists($file) && filemtime($file) > time() - $offset)
        return file_get_contents($file);

    $contents = file_get_contents($url);
    if ($contents === false)
        return false;

    file_put_contents($file, $contents);
    return $contents;
Wednesday, March 31, 2021
answered 7 Months ago

Under Windows it seems that files in vendor/bin are actually batch files, invoking the original file (and not php files which phpdbg will understand).

In this case:

dir=$(d=${0%[/\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)
"${dir}/phpunit" "$@"

i.e. ../phpunit/phpunit/phpunit (this path is relative to vendor/bin); thus the actual file is at vendor/phpunit/phpunit/phpunit.

And you can invoke it directly via phpdbg -qrr vendor/phpunit/phpunit/phpunit then.

Saturday, May 29, 2021
answered 5 Months ago

Put the entire string in double quotes since spaces are not allowed in command line. In the following i enclosed both usages of $label in double-quotes using the " escape sequence:

$cmd = " -background transparent -font $font -pointsize $size label:"$label" -stroke black -strokewidth 2 ".
"  ( -clone 0 -tile "" . $image . "[0]" -stroke black -strokewidth 2 -gravity center -annotate +0+0 "$label" ) ".
Saturday, May 29, 2021
answered 5 Months ago

This works in all browsers:

window.location.href = '...';

If you wanted to change the page without it reflecting in the browser back history, you can do:

Friday, June 4, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :