PHP从链接显示图像


PHP display image from link

我试图让我的图像通过链接点击时显示。然而,当我点击链接,它发现图像id显示在url链接ok,但不显示任何图像。你能帮帮我吗?

 <?php
//sets up thisPage          
 $pageSize=10;
  if (isset($_POST["thisPage"])) $thisPage=$_POST["thisPage"];
   else $thisPage=1;

 //selects all distinct expenses that have been uploaded
 $dbQuery="SELECT * FROM images WHERE user_id = '$userID' ";
  $dbResult=mysqli_query($db_connection, $dbQuery) or die(mysqli_error($db_connection));
  echo "<table cellspacing='"5'" class='"recordsTableBG'"> <thead 
  class='"recordsTableHeader'">";
  echo '<tr>     <th>ID</th><th>Amount</th><th>Description</th><th>Filename</th>
   <th>Project ID</th><th>Status</th></tr></thead>';
    echo '<tr class="alternateRowColor">';
   '<tr>';
    while ($dbRow=mysqli_fetch_array($dbResult)){
    echo "<img src = 'uploaded/$image' width = '200' height = '200'>";
  // display row with expense
  echo '<td>'. $dbRow['id'] .'</td>';
 echo '<td>'. $dbRow['user_id']. '</td>';
     echo '<td><a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>
Click here to view image</a></td>';
    }
 echo "</table>";
 echo "</form>";

   ?>

    <!-- add submitt button
            close form  -->
       </div>

这里混合使用了两种方法,这就是混淆之处。

首先,注意

<img src='uploaded/$image' width = '200' height = '200'>

浏览器会看到类似

的内容
<img src='uploaded/picture_of_cat.jpg' width='200' height='200'>

并渲染一个200px * 200px的猫的图像。我猜那是你的缩略图。(顺便说一句,我注意到它没有包含在<td></td>中,即使它在表行中)。

在表的另一部分,你有

<a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>Click here to view image</a>

会呈现为类似

的内容
<a href='http://example.com/index.php?imageid=1234'>Click here to view image</a>

当用户点击该链接时,它将向服务器发出GET请求,其中imageid等于1234。服务器将如何处理imageid ?在你发布的代码中,没有。

你有两个选择:

  1. 如果你想保持链接的原样,你必须写一些代码来获取imageid值,找到合适的图像,并将其返回到浏览器作为图像数据 -这意味着设置适当的标头并将数据发送回二进制数据

  2. 更简单的方法是将链接中的URL替换为您在<img>标签中的URL -当用户单击它时,服务器将返回图像。