How do I get an image to display on a page when the path for the image is in a MySql database?
Here is the 'id' and (path) 'name' in the MySql table called "upload":
rec_id | name
--------+------------------------------------------------------
1 | C:\Apache Tomcat 5.0.28\htdocs\aja开发者_如何学Gox\images\blob1.jpg
2 | C:\Apache Tomcat 5.0.28\htdocs\ajax\images\blob3.jpg
3 | C:\Apache Tomcat 5.0.28\htdocs\ajax\images\blob2.jpg
Get the image ID from $_GET[]
, set the Content-type
with header()
, then use readfile()
.
You need to turn the image path into a web URL.
Fetch the desired image path from the table
Find out what your web root is (I'm guessing it's
C:\Apache Tomcat 5.0.28\htdocs
) and put it e.g. into a constantdefine("WEBROOT", "C:\....");
You could auto-detect the web root using DOCUMENT_ROOT but setting it manually is more reliable.Cut off the web root from the path
str_replace(WEBROOT, "", $image_path);
Voilá! You have a relative URL to your image that you can output:
<img src='/ajax/images/blob1.jpg'>
精彩评论