php - 如何让存储在服务器上的图像在我的浏览器中正确查看?

标签 php mysql image filepath

到目前为止,我可以将图像上传到文件夹并将名称保存在数据库中。我想要做的是能够在浏览器中查看每个图像,并在其旁边或下方使用相应的名称。但我所拥有的是错误的图像分配了错误的名称。我浏览器中的第一张图片在数据库的记录中被分配了接下来的两个名字。我究竟做错了什么?我需要帮助。

这是我的代码:

添加页面:-

    <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td height="100" align="center" bgcolor="#CC0000" class="header_title">Facilitators Panel
          <table width="600" height="30" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="448" class="header_title" height="20"></td>
              <td width="92" class="text2"><a href="cpanel.php?user=<?php echo $row_rsadmin['aID']; ?>">Control Panel</a></td>
              <td width="60" class="text2">Logout</td>
            </tr>
          </table></td>
      </tr>
      <tr>
        <td height="300" valign="top" bgcolor="#666666" class="text2"><table width="640" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td class="text2"><a href="adfacilitators.php?user=<?php echo $row_rsadmin['aID']; ?>">Back</a></td>
          </tr>
          <tr>
            <td class="text2">&nbsp;</td>
          </tr>
        </table>
          <table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td width="640" height="40" colspan="4" align="center"><form action="fac.php" method="post" enctype="multipart/form-data" name="fac" id="fac">
      <table width="580" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td height="1" align="right">&nbsp;</td>
        </tr>
        <tr>
          <td width="132" align="right" class="text3">Name:</td>
          <td width="10" rowspan="4">&nbsp;</td>
          <td colspan="2"><label>
            <input name="name" type="text" class="textbox" id="name" />
          </label></td>
        </tr>
        <tr>
          <td height="45" align="right"><p class="text3">Photo:</p></td>
          <td width="272" height="45"><input type="hidden" name="MAX_FILE_SIZE" value="256000" />
            <input name="photo" type="file" id="photo" size="26" /></td>
          <td width="166">&nbsp;</td>
        </tr>
        <tr>
          <td height="1" align="right">&nbsp;</td>
        </tr>
        <tr>
          <td height="40" align="right">&nbsp;</td>
          <td colspan="2" valign="top"><label>
            <input name="button" type="submit" class="readmore" id="button" value="Submit" />
          </label>
            <input name="button2" type="reset" class="readmore" id="button2" value="Reset" /></td>
        </tr>
      </table>
    </form></td>
          </tr>
          <tr>
            <td height="10" colspan="4" align="center"></td>
          </tr>
          </table></p></td></tr>
    </table>

处理页面:-

    <?php require("../Connections/connMain.php"); ?>
    <?php

                // validation... since this is an image upload script we should run a check   
                // to make sure the uploaded file is in fact an image. Here is a simple check: 
                // getimagesize() returns false if the file tested is not an image. 
                if ($image = @getimagesize($_FILES['photo']['tmp_name'])&& ($_FILES["photo"]["size"]<= 2560000))
                {}
                else
                {
                    header("Location:error1.php");

                exit;
                }


       //This gets all the other information from the form 
      $name = htmlspecialchars($_POST['name']);
      $pix= ($_FILES['photo']['name']);
      $tmpName = $_FILES['photo']['tmp_name']; 


     //Writes the information to the database 
     mysql_query("INSERT INTO facilitators (photo, name) VALUES ('$pix', '$name')"); 


     //This is the directory where images will be saved 
     // Make sure the file was sent without errors
     //if($_FILES['photo']['error'] == 0) {
     $target = "../facilitators/";

     $target = $target  . basename( $_FILES['photo']['name']); 


     //Writes the photo to the server 
     if(move_uploaded_file($tmpName, $target)) 
     { 

     header("Location:adfacilitators.php");

     }
     else { 

     header("Location:error2.php");

     }
    ?>

输出页面:

<div id="wrapper2">
    <div id="headercont"><?php require_once('header.html'); ?></div>
    <div id="bannercont"><?php require_once('banner.html'); ?></div>
    <div id="mainbody">
      <div id="sidelink"><?php require_once('sidelink.html'); ?></div>
        <div id="mainbodyr">
          <div class="clear_4"></div>
          <div id="subheadertext">Facilitators</div>
          <div class="clear_4"></div>
          <div id="mainbodycontentcont">
            <?php do { ?>
            <table width="520" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="150"><div id="mainbodypiccont">
                  <? $info = mysql_fetch_array( $rsFacilitator ); echo "<img src=http://localhost/youngatart/facilitators/".$info['photo'] ." width='150'>" ?>
                </div></td>
                <td width="370"><div id="mainbodytextcont">
                  <div id="text1"><?php echo $row_rsFacilitator['name']; ?></div>
                </div></td>
              </tr>
              <tr>
                <td height="20" colspan="2"></td>
              </tr>
            </table>
              <?php } while ($row_rsFacilitator = mysql_fetch_assoc($rsFacilitator)); ?>
          </div>
        </div>
        <div class="clear_5"></div>
    </div>
    </div>
</body>

最佳答案

检查这个,我稍微修改了你的代码,

<div id="mainbodycontentcont">
            <?php while ($row_rsFacilitator = mysql_fetch_assoc($rsFacilitator) { ?>
            <table width="520" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="150"><div id="mainbodypiccont">
                  <? echo "<img src=http://localhost/youngatart/facilitators/".$row_rsFacilitator['photo'] ." width='150'>" ?>
                </div></td>
                <td width="370"><div id="mainbodytextcont">
                  <div id="text1"><?php echo $row_rsFacilitator['name']; ?></div>
                </div></td>
              </tr>
              <tr>
                <td height="20" colspan="2"></td>
              </tr>
            </table>
              <?php }?>
          </div>

编辑 我不完全理解你的问题,但如果你想从图像中编辑你的数据,请单击只需提供 <a>标记到图像。

<div id="mainbodycontentcont">
                <?php while ($row_rsFacilitator = mysql_fetch_assoc($rsFacilitator)) { ?>
                <table width="520" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="150"><div id="mainbodypiccont">
                      <? echo "<a href='yourfilename.php?imageid=1'><img src=http://localhost/youngatart/facilitators/".$row_rsFacilitator['photo'] ." width='150'></a>" ?>
                    </div></td>
                    <td width="370"><div id="mainbodytextcont">
                      <div id="text1"><a href='yourfilename.php?imageid=1'><?php echo $row_rsFacilitator['name']; ?></a></div>
                    </div></td>
                  </tr>
                  <tr>
                    <td height="20" colspan="2"></td>
                  </tr>
                </table>
                  <?php }?>
              </div>

关于php - 如何让存储在服务器上的图像在我的浏览器中正确查看?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12274598/

相关文章:

mysql - MySQL 上的子查询

python - 从 Gtk3+ 中的 base64 编码图像加载图像

php - 为 MySql 行提供几个唯一的 ID,并使用 php 显示具有一个特殊 ID 的所有行

php - 无法在 PHP 中触发 SQL 请求

mysql - 是否可以使用 join 和 union 进行查询?

swift - 在 swift 4 中使用 Base64 将图像转为字符串

ListView 中的 C# 图像

php - HTML/FPDF 表

PHP MYSQL 如何自动向表添加值

MySQL:按两列排序,最后是 NULL?