下面的代码是我的表单操作,但是当成功登录时,所有数据都显示在我的登录页面上,但它不显示图像,而是显示图像的名称。
<?php
// Connect to the database
require('config.php');
// Set username and password variables for this script
$user = mysql_real_escape_string($_POST["username"]);
$pass = mysql_real_escape_string($_POST["password"]);
// Make sure the username and password match, selecting all the client's
// data from the database if it does. Store the data into $clientdata
$clientdata = mysql_query("SELECT * FROM $tbl_name WHERE username='$user' and password='$pass'")
or die (mysql_error());
// Put the $clientdata query into an array we can work with
$data = mysql_fetch_array($clientdata, MYSQL_ASSOC);
// If the username and password matched, we should have one entry in our
// $clientdata array. If not, we should have 0. So, we can use a simple
// if/else statement
if(mysql_num_rows($clientdata) == 1){
$_SESSION['username'] = $user;
$_SESSION['firstname'] = $data['firstname'];
$_SESSION['lastname'] = $data['lastname'];
$_SESSION['address'] = $data['address'];
$_SESSION['nationality'] = $data['nationality'];
$_SESSION['id'] = $data['id'];
$_SESSION['photo']=$data['photo'];
?>
<?php
}else{echo "Click Login to try again or Register for an account.
Thank You";}
?>
<tr>
<td bgcolor="#999999">Passport</td>
<td bgcolor="#FFFFFF"><p><?php echo "" . $_SESSION['photo'] . ""; ?></p></tr>
答案 0 :(得分:0)
您说它打印出图像的名称,图像存储在图像目录中。我猜这意味着$_SESSION[ 'photo' ]
包含图像文件的路径。在这种情况下,您需要正确地将其包装在HTML中。
<img src="<?php echo $_SESSION[ 'photo' ] ?>" />
或者您上传的图片可能位于图片文件夹中,您只能将实际文件名存储在$_SESSION[ 'photo' ]
中,然后执行此操作。
<img src="/your-images-folder/<?php echo $_SESSION[ 'photo' ] ?>" />
如果图像存储在您的Web根目录之外,那么您需要将它们复制到您的Web根目录,或者您需要设置PHP脚本来接收未处理的图像请求并拉入文件从您上传的图像文件夹那样。
您可以通过在服务器配置(例如Apache的.conf文件)中添加URL重写规则来实现此目的,这样任何对图像文件实际不存在的/uploaded-images/*
的请求都会得到重写为PHP脚本。然后,您的PHP脚本可以在uploads文件夹中找到正确的图像,并使用header()
和file_get_contents()
函数,使用正确的mime类型将其显示回用户。