Hey guys currently I am thinking of serving images using an image handler script. I have two sources of images. One is from my web images folder where images that are used to construct my site interface are served. The other is in each users images folder where they can store their own images. I was thinking of giving each user image a unique id and then searching that id with the image handler script and serving the image, and changing the file name. The problem is that my site images folder does not have any information in the database and thus has no ids, should I just serve directly? Also this way of serving user images does not seem like the most efficient. If anyone has any suggestions I would really appreciate it.
$sql="SELECT username,file_name FROM images WHERE id=?";
$stmt=$conn->prepare($sql);
$result=$stmt->execute(array($ID));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$image_name = $row['file_name'];
$username = $row['username'];
}
$path="$username/images/$image_name";
header("Expires: -1");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
readfile($path);
The most obvious efficient way it to forgo PHP and let it be served directly by Apache, with caching. Is there any reason you have to keep track of images in this way you can’t do with just parsing access_log’s after the fact? And why no caching, I assume a new image gets a new unique id, not a rehashed old one?
That being said: if you do need database retrieval & serve it with php, it is about as efficient as you can get, although be very war of folders named by usernames, a possible security issue. Maybe cache the query result somewhere in memcached or the like, that’s about it.