What is an efficient way to serve images (PHP)?
<p>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.</p>

<pre><code>$sql="SELECT username,file_name FROM images WHERE id=?";
$stmt=$conn-&gt;prepare($sql);
$result=$stmt-&gt;execute(array($ID));

while($row = $stmt-&gt;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);
</code></pre>
