Jump to content

[SOLVED] preloading images - help...


quintus

Recommended Posts

Hey,

 

Im pretty new to php and i am looking to write a little script. My idea was to have a script which i could give a folder like "http://www.mydomain.com/images/" and it would then find every file in that folder or any subfolders and preload them all. This way after a short pause when loading the page, the entire thing loads a lot faster because of the cashed images.

 

I've beel fooling around with this for a few days, but most of my scripts dont even run let alone do what i want.

 

Any ideas? Has somone already done something like this?

 

--

Thomas

Link to comment
https://forums.phpfreaks.com/topic/45704-solved-preloading-images-help/
Share on other sites

$aray_files_directories = glob("dir/*");

echo "<pre>";

print_r($aray_files_directories);

---------------------------------------------------------------

glob

(PHP 4 >= 4.3.0, PHP 5)

 

glob -- Find pathnames matching a pattern

Description

array glob ( string pattern [, int flags] )

 

 

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells. No tilde expansion or parameter substitution is done.

 

Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.

 

Valid flags:

 

 

GLOB_MARK - Adds a slash to each item returned

 

GLOB_NOSORT - Return files as they appear in the directory (no sorting)

 

GLOB_NOCHECK - Return the search pattern if no files matching it were found

 

GLOB_NOESCAPE - Backslashes do not quote metacharacters

 

GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'

 

Note: This flag is not available on some non GNU systems, like Solaris.

 

GLOB_ONLYDIR - Return only directory entries which match the pattern

 

Note: Before PHP 4.3.3 GLOB_ONLYDIR was not available on Windows and other systems not using the GNU C library.

 

GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored. Added in PHP 5.1.0.

 

 

Example 1. Convenient way how glob() can replace opendir() and friends.

 

<?php

foreach (glob("*.txt") as $filename) {

    echo "$filename size " . filesize($filename) . "\n";

}

?> 

 

The above example will output something similar to:

 

funclist.txt size 44686

funcsummary.txt size 267625

quickref.txt size 137820

 

 

 

 

Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.

 

Note: This function isn't available on some systems (e.g. old Sun OS).

 

 

Thanks so much,

 

It works great.

 

The code i use is:

 

<p>
<?php
  // this preloads all images for this site and should only be on the main page...
  foreach (glob("images/*/*.*") as $filename)
  {
    echo '<img src="http://www.yggonline.com/' . $filename . '" class="preloadPic">';
  }
?>
</p> 

 

It works like a charm. Ive tested this vs without this on browsers with the cashe cleared and without this script each page load took around another 8 seconds to load, but with this script, the very first page loaded in 8 seconds and then spent another 10 seconds or so working afterwards and then after this i was able to surf the WHOLE site with 1/4 second page reloads. Thanks so much for the help!!

 

(btw, i created a css element called preloadpic which turned the image display off to make this work)

 

Thanks again!

 

--

Thomas

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.