【问题标题】:virtual img src to real path虚拟 img src 到真实路径
【发布时间】:2014-11-28 03:40:45
【问题描述】:

我为数据库表中的每个图像分配了一个 ID,该 ID 还具有图像的真实路径,包括真实名称,如

Table images {
id = '23432.jpg',
path = 'foo/bar',
name = 'baz.jpg' }

是否可以保留 img src="2343.jpg" 并从路径 [path/name] 获取图像而不使用 'data:image/jpg;base64' 仅表 id[23432.jpg]?

或者它是一个 .htaccess 作品!

【问题讨论】:

  • 你的问题并没有明确你想要达到的目标。
  • <img src="script.php?2343.jpg">script.php 读取并输出具有相应content-type 标头的图像文件的内容。或者,是的,您可以使用 mod_rewrite 隐藏对script.php的请求

标签: php image .htaccess


【解决方案1】:

<img src="/images/1234.jpg">

RewriteEngine On
RewriteRule ^images/(.*)$ script.php?$1 [L]

将所有到 subdir images 的请求重定向到 script.php 并放入 query_string 请求的文件名。您只需要在脚本中获取它,在 DB 中找到相应的图像,从磁盘读取它并使用正确的 content-type 标头输出。如果图像不在数据库中,则输出 404 错误。

【讨论】:

    【解决方案2】:

    这里假设path变量有图片的路径,名字是图片文件的名字。

    所以,你必须先给 mysql 查询才能从表中获取路径和图像名称。

    $q1="select path,name from images where id=".$id;
    $rs=mysql_query($q1);
    
    
     while($r=mysql_fetch_array($rs))
     {
          $path=$r[0];
          $imgname=$r[1];
    
          $completeimgpath=$path."/".$imgname;
          echo "<img src=".$completeimgpath." width=300 height=300 />"
       }
    

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2021-02-12
      • 2018-03-31
      • 2014-01-20
      • 2012-09-29
      相关资源
      最近更新 更多