What is an efficient way to serve images (PHP)?
- by Scarface
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);