开发者

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.

  1. Fetch the desired image path from the table

  2. 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 constant define("WEBROOT", "C:\...."); You could auto-detect the web root using DOCUMENT_ROOT but setting it manually is more reliable.

  3. Cut off the web root from the path str_replace(WEBROOT, "", $image_path);

  4. Voilá! You have a relative URL to your image that you can output: <img src='/ajax/images/blob1.jpg'>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜