Php 如何在浏览器中正确查看存储在服务器上的图像?
通过我目前所做的工作,我可以将图像上传到文件夹,并将名称保存在数据库中。我想做的是能够在浏览器中查看每个图像,并在其旁边或下面显示相应的名称。但我看到的是错误的图像,它被赋予了错误的名称。“我的浏览器”中的第一个图像被指定为数据库中记录中的下两个名称。我做错了什么?求你了,我需要帮助 这是我的密码: 添加页面:-Php 如何在浏览器中正确查看存储在服务器上的图像?,php,mysql,image,filepath,Php,Mysql,Image,Filepath,通过我目前所做的工作,我可以将图像上传到文件夹,并将名称保存在数据库中。我想做的是能够在浏览器中查看每个图像,并在其旁边或下面显示相应的名称。但我看到的是错误的图像,它被赋予了错误的名称。“我的浏览器”中的第一个图像被指定为数据库中记录中的下两个名称。我做错了什么?求你了,我需要帮助 这是我的密码: 添加页面:- <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">
<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> </td>
</tr>
<tr>
<td class="text2"><a href="adfacilitators.php?user=<?php echo $row_rsadmin['aID']; ?>">Back</a></td>
</tr>
<tr>
<td class="text2"> </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"> </td>
</tr>
<tr>
<td width="132" align="right" class="text3">Name:</td>
<td width="10" rowspan="4"> </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"> </td>
</tr>
<tr>
<td height="1" align="right"> </td>
</tr>
<tr>
<td height="40" align="right"> </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");
}
?>
主持人小组
注销
姓名:
照片:
处理页面:-
<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> </td>
</tr>
<tr>
<td class="text2"><a href="adfacilitators.php?user=<?php echo $row_rsadmin['aID']; ?>">Back</a></td>
</tr>
<tr>
<td class="text2"> </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"> </td>
</tr>
<tr>
<td width="132" align="right" class="text3">Name:</td>
<td width="10" rowspan="4"> </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"> </td>
</tr>
<tr>
<td height="1" align="right"> </td>
</tr>
<tr>
<td height="40" align="right"> </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");
}
?>
很好地看到了您的代码,您首先插入了记录,然后上载了文件。试着倒过来做。首先上传文件。如果成功,请将图片名称指定给变量$pix
,然后在数据库中插入内容。并检查文件夹中的文件是否已成功上载到目录中。请尝试将此文件的名称与刚刚插入数据库的名称匹配
此外,请注意:
- 避免使用不推荐的
mysql\uu
函数,选择mysqli\u
或PDO
- 使用参数化查询
检查一下,我对你的代码做了一些修改
<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>
你们查过数据库了吗。数据插入正确吗?数据插入正确,pic上传到服务器上的文件夹。我需要的是能够根据数据库中的id在一行上输出数据及其相应的图像,以便我以后可以根据其id和照片编辑每个项目名称。感谢您的回复。但是我在你代码的第二行有一个语法错误(刚才看到了问题所在。代码行缺少一个封闭的括号。非常感谢。你帮了我很大的忙。现在它工作正常。更进一步,如果我想通过单击链接编辑每个条目,该链接将我带到一个包含绑定到每个表单元素的表单的页面,请说文本字段绑定到名称字段在数据库中,文件字段绑定到database的photo字段,然后我可以编辑该特定条目的名称和图片,也可以删除。我可以单独使用文本,但不能使用图像。有什么帮助吗please@AgbogidiMichael我已经编辑了我的代码。虽然我不能理解你的问题,但是如果他不是你想要的告诉我所有你的帮助。我希望做的是,在每个名字之外都有一个编辑链接,当我点击它时,它会带我到一个有一个文本字段、文件字段和编辑按钮的表单页面。我想表单字段应该根据从数据库传递的id从数据库表中获得它们的值特定表项。表单代码下方:也感谢您的输入。在输入数据库之前,我将图像切换并上载到服务器。这有助于解决同名图像的覆盖问题。我还添加了rand()和time()函数来唯一标识每个图像。